hotshot/
helpers.rs

1use tracing_subscriber::{fmt::format::FmtSpan, EnvFilter};
2
3/// Initializes logging
4pub fn initialize_logging() {
5    // Parse the `RUST_LOG_SPAN_EVENTS` environment variable
6    let span_event_filter = match std::env::var("RUST_LOG_SPAN_EVENTS") {
7        Ok(val) => val
8            .split(',')
9            .map(|s| match s.trim() {
10                "new" => FmtSpan::NEW,
11                "enter" => FmtSpan::ENTER,
12                "exit" => FmtSpan::EXIT,
13                "close" => FmtSpan::CLOSE,
14                "active" => FmtSpan::ACTIVE,
15                "full" => FmtSpan::FULL,
16                _ => FmtSpan::NONE,
17            })
18            .fold(FmtSpan::NONE, |acc, x| acc | x),
19        Err(_) => FmtSpan::NONE,
20    };
21
22    // Conditionally initialize in `json` mode
23    if std::env::var("RUST_LOG_FORMAT") == Ok("json".to_string()) {
24        let _ = tracing_subscriber::fmt()
25            .with_env_filter(EnvFilter::from_default_env())
26            .with_span_events(span_event_filter)
27            .json()
28            .try_init();
29    } else {
30        let _ = tracing_subscriber::fmt()
31            .with_env_filter(EnvFilter::from_default_env())
32            .with_span_events(span_event_filter)
33            .try_init();
34    };
35}