Creating and using multiple loggers


#1

Hello, I’m trying to use fern for logging, and cannot find out how to get more than one logger working. Initializing the main logger is easy and straightforward, since there is a simple function that enables the use of macros such as info!() or debug!(). The thing is that I need to use more than one log, for which I do the following:

    let log_path = format!("data/logs/GSM/Battery.{}.log",
                           time::now_utc()
                               .strftime("%Y-%m-%d.%H-%M-%S")
                               .unwrap());
    let logger = fern::DispatchConfig {
                     format: Box::new(|msg: &str,
                                       level: &log::LogLevel,
                                       _location: &log::LogLocation| {
                         format!("[{}][{}] {}",
                                 time::now_utc().strftime("%Y-%m-%d][%H:%M:%S").unwrap(),
                                 level,
                                 msg)
                     }),
                     output: vec![fern::OutputConfig::file(&log_path)],
                     level: log::LogLevelFilter::Info,
                 }
                 .into_fern_logger()
                 .unwrap();

The problem is that I don’t know how to use that Logger. It requires a message, a log level and a log::LogLocation which I don’t know how to generate. The other possible option, creating a log::Log has another problem: it needs a log::LogRecord that I don’t know how to generate either. What can I do?


#2

LogRecord and LogLocation can only be created by the log crate internals. I am not familiar with fern, but you’d presumably want to use the init_global_logger function and then use the macros provided by the log crate to generate messages.


#3

I just received a reply from the creator of the library and it seems it’s not possible to do multiple loggers at the same time :confused: No worries, I’ll implement another thing :slightly_smiling: