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 = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(level));
11
12    let result = match format {
13        LogFormat::Pretty => tracing_subscriber::registry()
14            .with(env_filter)
15            .with(fmt::layer().pretty())
16            .try_init(),
17        LogFormat::Json => tracing_subscriber::registry()
18            .with(env_filter)
19            .with(fmt::layer().json().flatten_event(true))
20            .try_init(),
21        LogFormat::Compact => tracing_subscriber::registry()
22            .with(env_filter)
23            .with(fmt::layer().compact())
24            .try_init(),
25    };
26
27    // Ignore error if already initialized (happens in tests)
28    let _ = result;
29}