Drakulix / simplelog.rs

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

SimpleLogger and TermLogger give interleaved output when used from threads #22

Closed hansjorg closed 6 years ago

hansjorg commented 6 years ago

When logging from two different threads, statements written to stdout and stderr might become interleaved.

Example:

#[macro_use]
extern crate log;
extern crate simplelog;

use simplelog::{LevelFilter, SimpleLogger};

use std::{thread, time};

fn main() {

    let _ = SimpleLogger::init(LevelFilter::Debug, simplelog::Config::default());

    thread::spawn(move || {
        loop {
          info!("from thread 1");
        }
    });

    thread::spawn(move || {
        loop {
          error!("from thread 2");
        }
    });

    info!("starting");
    thread::sleep(time::Duration::from_millis(1000));
}

Example output:

19:51:07 [INFO] starting
19:51:07 [INFO] from thread 1
19:51:07 [INFO] from thread 1
19:51:07 [INFO] from thread 1
19:51:019:51:07 [INFO] from thread 1
7 [ERROR] from thread 2
19:51:07 [INFO] from thread 1
19:51:07 19:51:07 [INFO] from thread 1
[ERROR] from thread 2
1919:51:07 [INFO] from thread 1
:51:07 [19:51:07 [INFO] from thread 1
ERROR] from thread 2
1919:51:07 [INFO] from thread 1