file_single_thread_two_threads/
two-threads.rs

1use std::{path::PathBuf, thread};
2
3use rslogger::Logger;
4use log::{info, warn, error};
5
6/// With this configuration, each thread is writing on the file separately.
7/// The logging is happening respectively on Thread 1 and Thread 2 they will contend the resource 
8/// of the file to write on it.This is not optimal in this case.
9fn main() {
10    Logger::new()
11        .with_level(log::LevelFilter::Trace)
12        .with_local_timestamps()
13        .with_thread()
14        .add_writer_file(
15            PathBuf::from("./LOGS/two-threads.log"), 
16            false, None)
17        .init().unwrap();
18
19    let handler1 = thread::Builder::new().name("Thread 1".to_string()).spawn(move | | {
20        for i in 0..10 {
21            info!("This is a info, i: {i}");
22        }
23    }).unwrap();
24
25    let handler2 = thread::Builder::new().name("Thread 2".to_string()).spawn(move | | {
26        for i in 0..10 {
27            info!("This is a info, i: {i}");
28        }
29    }).unwrap();
30    
31    warn!("Warning on main!");
32
33    handler1.join().unwrap();
34    handler2.join().unwrap();
35
36    log::logger().flush();
37}