Log4rs - how to use different levels for appenders

Is it possible to use a specific logging level for a particular appender, let's say trace for console and error for a file?

Yes, this is possible using the filters parameter when you configure an appender (docs). Here's an example:

[appenders.stdout]
kind = "console"
filters = [ { kind = "threshold", level = "info" } ]

[appenders.logfile]
kind = "file"
path = "events.log"
filters = [ { kind = "threshold", level = "trace" } ]

[root]
appenders = ["stdout", "logfile"]

Thanks @adeschamps, that's exactly what I was looking for. Here's also how to set it up in code without yaml:

    let config = Config::builder()
        .appender(
            Appender::builder()
                .filter(Box::new(ThresholdFilter::new(LevelFilter::Info)))
                .build("console", Box::new(console)),
        )
        ...
1 Like