emabee / flexi_logger

A flexible logger for rust programs that can write to stderr, stdout, and/or to log files
Apache License 2.0
315 stars 55 forks source link

FileLogWriter does not follow its max_level #162

Closed JoeWildfong closed 5 months ago

JoeWildfong commented 5 months ago

In this example code:

use flexi_logger::{writers::FileLogWriter, FileSpec, LogSpecification, Logger};
use log::{error, trace, LevelFilter};

fn main() {
    let writer = FileLogWriter::builder(FileSpec::default())
        .max_level(LevelFilter::Warn)
        .try_build()
        .unwrap();

    let _handle = Logger::with(LogSpecification::trace())
        .log_to_writer(Box::new(writer))
        .start()
        .unwrap();

    trace!("this should not be written to the file");
    error!("this should be written to the file");
}

I would expect that only the error! message gets written to the log file, but both are written when I run this. It looks like FileLogWriter never checks that a record is above its own max_level before writing it.

I'd be happy to PR a fix for this if you'd like.

emabee commented 5 months ago

Right, that's really missing! Obviously there is no test for that 🥸 The PR should also bring a test 😇. Have fun!