rust-lang / log

Logging implementation for Rust
https://docs.rs/log
Apache License 2.0
2.18k stars 252 forks source link

log crate catches Clion stop signal #614

Closed lioriz1 closed 8 months ago

lioriz1 commented 8 months ago

I'm using Clion developing in a remote environment. When I'm stoping the process from the clion stop the logs stops but the process not.

fn init_logger() -> Result<(), String> {
    env_logger::Builder::new()
        .format(|buf, record| {
            writeln!(
                buf,
                "{}:{} {} [{}]: {}",
                record.file().unwrap_or("unknown"),
                record.line().unwrap_or(0),
                chrono::Local::now().format("%Y-%m-%d %H:%M:%S.%3f"),
                record.level(),
                record.args()
            )
        })
        .filter(None, LevelFilter::Debug)
        .is_test(true)
        .init();
    Ok(())
}

fn main() -> Result<(), String>  {
    let _ = init_logger();
    info!("Hello, world!");
    for i in 0..10 {
        info!("{}", i);
        // spdlog::info!("{}", i);
        // println!("{}", i);
        thread::sleep(Duration::from_secs(1));
    }
    return Ok(());
}

In this example when I'm stopping the process in the middle the logs will stop but the loop will run to the end, the full 10 seconds and the process will exit successfully.

If I change from log::info! to spdlog::info! or from log::info! to println! or adding .is_test(true) to log init, it works as expected, exits imidietly with fail code.

If I run in regular Clion run on my machine compiler log works fine the same as the other loggers but I would expect it to work the same in my remote environment.

Is there a reason for this behavior? What is the .is_test(true) flag?

Thomasdezeeuw commented 8 months ago

The log crate does not handle any process signals, so that functionality is coming from another crate.

lioriz1 commented 8 months ago

Thanks @Thomasdezeeuw Trying rust-cli/env_logger crate