Skip to main content

slop_utils/
logger.rs

1use std::sync::Once;
2
3use tracing_forest::ForestLayer;
4use tracing_subscriber::{
5    fmt::format::FmtSpan, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry,
6};
7
8static INIT: Once = Once::new();
9
10/// A simple logger.
11///
12/// Set the `RUST_LOG` environment variable to be set to `info` or `debug`.
13pub fn setup_logger() {
14    INIT.call_once(|| {
15        let default_filter = "off";
16        let env_filter = EnvFilter::try_from_default_env()
17            .unwrap_or_else(|_| EnvFilter::new(default_filter))
18            .add_directive("hyper=off".parse().unwrap())
19            .add_directive("p3_keccak_air=off".parse().unwrap())
20            .add_directive("p3_fri=off".parse().unwrap())
21            .add_directive("p3_dft=off".parse().unwrap())
22            .add_directive("p3_challenger=off".parse().unwrap());
23
24        // if the RUST_LOGGER environment variable is set, use it to determine which logger to
25        // configure (tracing_forest or tracing_subscriber)
26        // otherwise, default to 'forest'
27        let logger_type = std::env::var("RUST_LOGGER").unwrap_or_else(|_| "flat".to_string());
28        match logger_type.as_str() {
29            "forest" => {
30                Registry::default().with(env_filter).with(ForestLayer::default()).init();
31            }
32            "flat" => {
33                tracing_subscriber::fmt::Subscriber::builder()
34                    .compact()
35                    .with_file(false)
36                    .with_target(false)
37                    .with_thread_names(false)
38                    .with_env_filter(env_filter)
39                    .with_span_events(FmtSpan::CLOSE)
40                    .finish()
41                    .init();
42            }
43            _ => {
44                panic!("Invalid logger type: {logger_type}");
45            }
46        }
47    });
48}