-
Notifications
You must be signed in to change notification settings - Fork 130
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
Dynamically change log level after init
#228
Comments
I'd like to mention that I have a similar use case. I have tried to disable logging before initializing the builder but it doesn't work. I've also tried to send all the log to a sink target, but it doesn't work either. It seems that the RUST_LOG env. variable takes priority over the settings of the builder... This is what I do:
|
You don't need |
the documentation says |
I think you might get strange results calling |
btw #113 looks somewhat related |
Currently the `--debug` flag does not actually enable debug logging as intended. We initialize `env_logger` immediately in `main`, then attempt to change the log filtering level in `NewCli::run` if the `--debug` flag is passed. In practice this does not work, as `env_logger::builder` needs `init` to be called to change the global logger. This is not an option, as we've already called `init`. The internet suggests[0] that using `log::set_max_level` lets us change the level after `init`, but it did not work when I tried it out. Move logger initialization into `NewCli::run` so that we know the desired log level ahead of calling `init`. This means that items logged before we initialize the logger will be lost, but in practice we're not missing anything. [0] rust-cli/env_logger#228 (comment)
Currently the `--debug` flag does not actually enable debug logging as intended. We initialize `env_logger` immediately in `main`, then attempt to change the log filtering level in `NewCli::run` if the `--debug` flag is passed. This does not work, as `env_logger::builder` needs its `init` method to be called to change the global logger. This is not an option, as we've already called `env_logger::init`. The internet suggests[0] that using `log::set_max_level` lets us change the level independently of logger initialization, but this did not work when I tried it out. Move logger initialization into `NewCli::run` so that we know the desired log level ahead of calling `init`. This means that items logged before we initialize the logger will be lost, but in practice we're not missing anything. [0] rust-cli/env_logger#228 (comment)
Is there currently a way to change the log level after having called
init
already? If there is currently no way, i would like to request this featureUse case:
i would like to enable logging as the first thing in my code to have log output (from env, etc) and later after having parsed the cli (like with clap) set the level based on something like
verbosity
.also another use case would be to dynamically turn the logging on / off for a specific part (which is probably already covered by #144)
The text was updated successfully, but these errors were encountered: