mecha10-runtime 0.6.2

Runtime supervisor for Mecha10 nodes - launching, monitoring, and lifecycle management
Documentation
//! Logging initialization and configuration

use crate::config::LogFormat;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};

/// Initialize logging with the specified format and level
///
/// Note: This can only be called once per process. Subsequent calls will be ignored.
pub fn init_logging(level: &str, format: LogFormat) {
    let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(level));

    let result = match format {
        LogFormat::Pretty => tracing_subscriber::registry()
            .with(env_filter)
            .with(fmt::layer().pretty())
            .try_init(),
        LogFormat::Json => tracing_subscriber::registry()
            .with(env_filter)
            .with(fmt::layer().json().flatten_event(true))
            .try_init(),
        LogFormat::Compact => tracing_subscriber::registry()
            .with(env_filter)
            .with(fmt::layer().compact())
            .try_init(),
    };

    // Ignore error if already initialized (happens in tests)
    let _ = result;
}