Skip to main content

mecha10_runtime/
logging.rs

1//! Logging initialization and configuration
2
3use crate::config::LogFormat;
4use tracing_subscriber::{fmt, prelude::*, EnvFilter};
5
6/// Initialize logging with the specified format and level
7///
8/// Note: This can only be called once per process. Subsequent calls will be ignored.
9pub fn init_logging(level: &str, format: LogFormat) {
10    let env_filter = std::env::var("LOG_LEVEL")
11        .ok()
12        .map(EnvFilter::new)
13        .or_else(|| EnvFilter::try_from_default_env().ok())
14        .unwrap_or_else(|| EnvFilter::new(format!("{level},rtc_ice=error,rtc::peer_connection::handler=error")));
15
16    let result = match format {
17        LogFormat::Pretty => tracing_subscriber::registry()
18            .with(env_filter)
19            .with(fmt::layer().pretty())
20            .try_init(),
21        LogFormat::Json => tracing_subscriber::registry()
22            .with(env_filter)
23            .with(fmt::layer().json().flatten_event(true))
24            .try_init(),
25        LogFormat::Compact => tracing_subscriber::registry()
26            .with(env_filter)
27            .with(fmt::layer().compact())
28            .try_init(),
29    };
30
31    let _ = result;
32}