Drakulix / simplelog.rs

Simple Logging Facility for Rust
https://docs.rs/simplelog/
Apache License 2.0
423 stars 71 forks source link

Terminal output not working when things are initialized from a crate #81

Closed parazyd closed 2 years ago

parazyd commented 2 years ago

I'm trying to have a general log initializing function that I can share between my binaries. However, it seems that terminal output does not happen in this case, and I'm not sure why. Any clues on how I can get this working so I get the logging both on the terminal and in my logfile? Suffice to say, it works if I use the lib.rs code in my binary directly, and not as an abstracted function in a crate.

lib.rs

use simplelog::*

pub fn init_logging(verbose: bool, log_path: &str) -> Result<(), log::SetLoggerError> {
    let debug_level = if verbose { LevelFilter::Debug } else {LevelFilter::Off };
    let logger_config = ConfigBuilder::new().set_time_format_str("%T%6.f").build();

    Ok(CombinedLogger::init(vec![
        SimpleLogger::new(debug_level, logger_config),
        WriteLogger::new(
            LevelFilter::Debug,
            Config::default(),
            std::fs::File::create(log_path).unwrap(),
        ),
    ])?)
}

bin/logme.rs

use log::debug;
use my_crate::init_logging;

fn main() -> Result<(), log::SetLoggerError> {
    init_logging(true, "/tmp/mylog.txt")?;
    debug!("I see this in my file but not on my terminal");
    Ok(())
}
m-lima commented 2 years ago

To expand more on the problem, which I believe to be the same problem, no terminal output logging is working in general. To confirm this, I ran the README.md example and got no terminal output. Also, none of the example binaries in the examples folder were able to output to the terminal, but file logging did work

m-lima commented 2 years ago

So simplelog = "=0.10.0" works with terminal output, but simplelog = "0.10.1" does not. Maybe a regression in ce44265b4bf88a06d551cf018270a85b71725aba?

Drakulix commented 2 years ago

Likely related to #82, can you test the current master branch? I will likely release 0.10.2 on crates.io later today.

m-lima commented 2 years ago

Likely related to #82, can you test the current master branch? I will likely release 0.10.2 on crates.io later today.

Master works with my previous tests 👍

Drakulix commented 2 years ago

0.10.2 is released and 0.10.1 yanked.