threads/
threads.rs

1use traccia::{LogLevel, Record};
2
3struct CustomFormatter;
4
5impl traccia::Formatter for CustomFormatter {
6    fn format(&self, record: &Record) -> String {
7        let id_str = format!("{:?}", record.thread_id);
8        let id_str = id_str.replace("ThreadId(", "").replace(")", "");
9
10        format!(
11            "[{}] [thread:{}] {}",
12            record.level.default_coloring(),
13            id_str,
14            record.message,
15        )
16    }
17}
18
19fn main() {
20    traccia::init_with_config(traccia::Config {
21        level: LogLevel::Trace,
22        format: Some(Box::new(CustomFormatter)),
23        ..Default::default()
24    });
25
26    let handles = (0..3)
27        .map(|i| {
28            std::thread::spawn(move || {
29                traccia::trace!("This is a trace message from thread {}", i);
30                traccia::debug!("This is a debug message from thread {}", i);
31                traccia::info!("This is an info message from thread {}", i);
32                traccia::warn!("This is a warn message from thread {}", i);
33                traccia::error!("This is an error message from thread {}", i);
34                traccia::fatal!("This is a fatal message from thread {}", i);
35            })
36        })
37        .collect::<Vec<_>>();
38
39    for handle in handles {
40        handle.join().unwrap();
41    }
42}