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

enabled() methods don't respect the target filter #144

Open
ajenkinski opened this issue Jan 23, 2024 · 0 comments
Open

enabled() methods don't respect the target filter #144

ajenkinski opened this issue Jan 23, 2024 · 0 comments

Comments

@ajenkinski
Copy link

The log_enabled! macro supports a target keyword, to check if logging is enabled with for a given target. The documentation gives this example for instance:

if log_enabled!(target: "Global", Debug) {
   let data = expensive_call();
   debug!(target: "Global", "expensive debug data: {} {}", data.x, data.y);
}

However, the above example would not work correctly when using simplelog as the logging implementation, because simplelogging's implementations of the enabled() method, which log_enabled! depends on, only checks the level, but not the target. So in the above example, if the "Global" target was being filtered out, but the current level was Debug, then the body of the if-statement would still execute. Note, the log! macros themselves do correctly filter on target in addition to level.

Reproduction:

use log::{debug, log_enabled, Level};
use simplelog::{ConfigBuilder, LevelFilter, SimpleLogger};

fn main() {
    SimpleLogger::init(
        LevelFilter::Debug,
        ConfigBuilder::new()
            .add_filter_allow_str("foo")
            .build(),
    )
    .unwrap();

    if log_enabled!(target: "bar", Level::Debug) {
        println!("This shouldn't be printed because only target `foo` is enabled, but it will be printed");
        debug!(target: "bar", "This will be correctly filtered out and won't print");
    }
}

The expected behavior is that the whole if-block body would be skipped, but in fact the body is executed.

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

No branches or pull requests

1 participant