Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a programmatic way to configure Picocli's TraceLevel #1471

Closed
ekinano opened this issue Nov 19, 2021 · 4 comments
Closed

Provide a programmatic way to configure Picocli's TraceLevel #1471

ekinano opened this issue Nov 19, 2021 · 4 comments

Comments

@ekinano
Copy link

ekinano commented Nov 19, 2021

The current pattern is to set the picocli.trace system property. It would be nice if there were a more programmatic way to do this, e.g. something like

cmdLine.setTraceLevel(OFF)

For context, we are currently using Picocli's parsing capabilities in an environment where a pattern like System.setProperty is heavily discouraged and requires a security exception.

@remkop
Copy link
Owner

remkop commented Nov 19, 2021

Hi @ekinano, thanks for raising this.

One could call System.setProperty in the main method before doing anything else to accomplish this programmatically, or am I missing something?

@ekinano
Copy link
Author

ekinano commented Nov 19, 2021

Thanks for the quick reply @remkop. System.setProperty works, but it is a highly discouraged practice at my company to the point where you need a security exception to be able to use it. While we can probably get that exception for our use-case, it seems better for us to avoid that conversation entirely if there can be another way of setting the trace level without using System.setProperty. It would be nice if that way would be through the Picocli objects directly like e.g. CommandLine. Hopefully that makes sense?

(If you're curious, we have a server for processing CLI-like arguments. We are leveraging Picocli's annotations to specify our "command" classes and its parser to parse those subcommands from the request. We would like to turn off Picocli's tracing to avoid filling our logs).

@remkop
Copy link
Owner

remkop commented Nov 25, 2021

@ekinano Thank you for the clarification. That makes sense.

Will you be able to provide a pull request for this?

@remkop
Copy link
Owner

remkop commented Feb 13, 2022

This has been implemented and will be included in the 4.7.0 release.
Thank you for the suggestion!

Details

(from the release notes):

From picocli 4.7.0, applications can programmatically set the trace level, and use tracing in custom components.

In addition to setting system property picocli.trace, applications can now change the trace level via the Tracer::setLevel method. For example:

CommandLine.tracer().setLevel(CommandLine.TraceLevel.INFO);

The new public method CommandLine.tracer() returns the singleton Tracer object that is used internally by picocli, and can also be used by custom component implementations to do tracing. For example:

class MyIntConverter implements ITypeConverter<Integer> {
    public Integer convert(String value) {
        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException ex) {
            CommandLine.tracer().info(
                    "Could not convert %s to Integer, returning default value -1", value);
            return -1;
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants