Drakulix/simplelog.rs

Silence reqwest via Config

aress31 opened this issue · 2 comments

I am looking at ways to silence reqwest, see seanmonstar/reqwest#1578 (reply in thread).

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(),
        ),
    ]);

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:

if !allowed.iter().any(|v| path.starts_with(&**v)) {

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

Brilliant, thanks @Drakulix !