emabee / flexi_logger

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

flexi_logger::writers ignore log level #128

Closed incker closed 1 year ago

incker commented 1 year ago

example form https://docs.rs/flexi_logger/0.24.1/flexi_logger/writers/index.html

I have added 2 lines to log::trace {Alert} And they were handeled, How to set log level global ??

use log::*;

use flexi_logger::{FileSpec,Logger};
use flexi_logger::writers::FileLogWriter;

// Configure a FileLogWriter for alert messages
pub fn alert_logger() -> Box<FileLogWriter> {
    Box::new(FileLogWriter::builder(
        FileSpec::default()
            .discriminant("Alert")
            .suffix("alerts")
    )
        .print_message()
        .try_build()
        .unwrap())
}

// Define a macro for writing messages to the alert log and to the normal log
#[macro_use]
mod macros {
    #[macro_export]
    macro_rules! alert_error {
        ($($arg:tt)*) => (
            error!(target: "{Alert,_Default}", $($arg)*);
        )
    }
}

fn main() {
    Logger::try_with_env_or_str("info")
        .expect("LogSpecification String has errors")
        .print_message()
        .log_to_file(FileSpec::default())
        .add_writer("Alert", alert_logger())
        .start()
        .unwrap_or_else(|e| panic!("Logger initialization failed with {}", e));

    // Explicitly send logs to different loggers
    error!(target : "{Alert}", "This is only an alert");
    error!(target : "{Alert,_Default}", "This is an alert and log message");

     // 2 LINES I HAVE ADDED ! THEY ARE HANDLED AND WRITTEN TO FILE ((((((((((((((((((((
    trace!(target : "{Alert}", "This is only an alert");
    trace!(target : "{Alert,_Default}", "This is an alert and log message");

    // Nicer: use the explicit macro
    alert_error!("This is another alert and log message");

    // Standard log macros write only to the normal log
    error!("This is a normal error message");
    warn!("This is a warning");
    info!("This is an info message");
    debug!("This is a debug message - you will not see it");
    trace!("This is a trace message - you will not see it");
}

Forward thank you

emabee commented 1 year ago

Weirdly, theFileLogWriterBuilder initialises the FileLogWriter it produces with some max_log_level, but does not allow you to specify that value, it sets it to always to TRACE. That can easily be changed - would it be good enough if you could set it during the initialization only?

incker commented 1 year ago

That can easily be changed - would it be good enough if you could set it during the initialization only?

I think yes..