Skip to main content

dag_executor/utils/
tracing.rs

1//! Structured logging / tracing setup.
2
3use std::sync::Once;
4use tracing_subscriber::{fmt, prelude::*, EnvFilter};
5
6static INIT: Once = Once::new();
7
8/// Initialize the global tracing subscriber.
9///
10/// Safe to call multiple times — only the first call takes effect (subsequent
11/// calls are no-ops), which keeps tests and examples from panicking on a double
12/// install. The filter is taken from `RUST_LOG` if set, otherwise from
13/// `default_level`.
14pub fn init(default_level: &str) {
15    INIT.call_once(|| {
16        let filter =
17            EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(default_level));
18        let registry = tracing_subscriber::registry().with(filter);
19
20        // Pretty, human-readable logs by default; switch to JSON when
21        // `DAG_LOG_JSON` is set (useful for log aggregation in production).
22        if std::env::var("DAG_LOG_JSON").is_ok() {
23            registry.with(fmt::layer().json()).init();
24        } else {
25            registry.with(fmt::layer().compact()).init();
26        }
27    });
28}