djugei / indicatif-log-bridge

bridges the log crate and indicatif to stop the progress bars and log lines from mixing up
8 stars 2 forks source link

`log::max_level()` set incorrectly when higher log levels are configured for some modules #4

Open bspot opened 3 months ago

bspot commented 3 months ago

How to reproduce:

At least env_logger supports setting a higher log level for some module than the default level:

let multi = indicatif::MultiProgress::new();
indicatif_log_bridge::LogWrapper::new(
    multi.clone(),
    env_logger::Builder::new()
        .filter_module(&"my_module", log::LevelFilter::Debug) // Set Debug for my module
        .filter_level(log::LevelFilter::Info) // Set Info as the default for all other modules
        .build()
).try_init().unwrap();

In this case, log::max_level() will be set to the default level:

println!("{:?}", log::max_level());
// outputs "Info"

This means that the directive for my_module will effectively be ignored. Debug messages from that module will not be output.

How to solve:

I don't believe there is a general way to fix this, since the log::Log trait does not provide a way to find out the max level for the logger.

An individual application can solve it by setting log::set_max_level(Trace) after initializing the log bridge at the cost of somewhat increased overhead.

However, it might be a good idea to explain this in the documentation.

djugei commented 3 months ago

on the one hand this is semi-documented:

https://docs.rs/indicatif-log-bridge/latest/indicatif_log_bridge/struct.LogWrapper.html#method.try_init

on the other you just opened my eyes to a bug in one of my own applications that is based on this behaviour :D.

i will see if i can do anything about this.

djugei commented 3 months ago

In fact this will probably be a pretty common issue if you want to have a low log level for your dependencies as default and raise it to debug/trace for the application/lib you are currently developing.

djugei commented 3 weeks ago

can you check if https://github.com/djugei/indicatif-log-bridge/commit/14bafa53d40e98e8b39f755d7bec7858e4a7780f#diff-b1a35a68f14e696205874893c07fd24fdb88882b47c23cc0e0c80a30c7d53759R38 fixes this for you?