niom-turn/src/logging.rs

29 lines
975 B
Rust

//! Logging helpers shared between the library, binaries, and integration tests.
//! Ensures we configure tracing once with sensible default filters while still
//! allowing `RUST_LOG` to override verbosity.
use std::sync::Once;
use tracing_subscriber::{fmt, EnvFilter};
static INIT: Once = Once::new();
/// Initialise tracing with a sane default filter (`warn` globally,
/// `niom_turn=info`) unless `RUST_LOG` is provided.
pub fn init_tracing() {
init_tracing_with_default("warn,niom_turn=info");
}
/// Initialise tracing with a custom default directive that can still be
/// overridden via `RUST_LOG` at runtime.
pub fn init_tracing_with_default(default_directive: &str) {
INIT.call_once(|| {
let env_filter =
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(default_directive));
fmt()
.with_env_filter(env_filter)
.with_target(false)
.compact()
.init();
});
}