Drakulix / simplelog.rs

Simple Logging Facility for Rust
https://docs.rs/simplelog/
Apache License 2.0
414 stars 71 forks source link

Silence reqwest via Config #107

Closed aress31 closed 2 years ago

aress31 commented 2 years ago

I am looking at ways to silence reqwest, see https://github.com/seanmonstar/reqwest/discussions/1578#discussioncomment-3172945.

Which led me to look at add_filter_ignore. However, I found nowhere in the documentation how to call add_filter_ignore on a Config::default().

This is my current code:

    let _ = CombinedLogger::init(vec![
        TermLogger::new(
            args.verbose.log_level_filter(),
            Config::default(),
            TerminalMode::Stdout,
            ColorChoice::Auto,
        ),
        WriteLogger::new(
            args.verbose.log_level_filter(),
            Config::default(),
            File::create(args.outfile.clone()).unwrap(),
        ),
    ]);

If you could help with this, that would be wonderful!

EDIT: I just managed to somehow silence reqwest, see the following, but surely there must be a more elegant solution - maybe using wildcards?

    let mut config_builder = ConfigBuilder::new();

    config_builder.add_filter_ignore_str("reqwest::connect");
    config_builder.add_filter_ignore_str("reqwest::async_impl::client");

    let _ = CombinedLogger::init(vec![
        TermLogger::new(
            args.verbose.log_level_filter(),
            config_builder.build(),
            TerminalMode::Stdout,
            ColorChoice::Auto,
        ),
        WriteLogger::new(
            args.verbose.log_level_filter(),
            config_builder.build(),
            File::create(args.outfile.clone()).unwrap(),
        ),
    ]);
Drakulix commented 2 years ago

Which led me to look at add_filter_ignore. However, I found nowhere in the documentation how to call add_filter_ignore on a Config::default().

Indeed, you can't do that. You are supposed to use the ConfigBuilder, if you want to adjust any default parameters. But it seems you figured that out already.

EDIT: I just managed to somehow silence reqwest, see the following, but surely there must be a more elegant solution - maybe using wildcards?

You can see the logic for skipping log messages here: https://github.com/Drakulix/simplelog.rs/blob/95275c88b14f204a5e8278cd98072e4ffb0bc7d8/src/loggers/logging.rs#L226

For simplicity and performance it is no fancy regex or anything supporting wildcards, but just a starts_with check for the path. So something like this should be sufficient:

let config = ConfigBuilder::new()
    .add_filter_ignore_str("reqwest")
    .build();

(Config implements Clone if you want to use the same config for multiple loggers.)

aress31 commented 2 years ago

Brilliant, thanks @Drakulix !