rust-cli / env_logger

A logging implementation for `log` which is configured via an environment variable.
https://docs.rs/env_logger
Apache License 2.0
797 stars 124 forks source link

Dynamically change log level after `init` #228

Open hasezoey opened 2 years ago

hasezoey commented 2 years ago

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 feature

Use 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)

wiz21b commented 2 years ago

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:

log_builder.filter(None, LevelFilter::Error);
log_builder.target(env_logger::Target::Pipe(sink));
log_builder.init();
morrisonlevi commented 2 years ago

You don't need env_logger to change the log level as the log crate already provides log::set_max_level.

hasezoey commented 2 years ago

You don't need env_logger to change the log level as the log crate already provides log::set_max_level.

the documentation says Generally, this should only be called by the active logging implementation., is env_logger fine with it being change outside of itself?

KodrAus commented 2 years ago

I think you might get strange results calling set_max_level directly, since env_logger "compiles" the max level into its filters, those aren't derived on-the-fly from the max level set in log.

epage commented 1 year ago

btw #113 looks somewhat related