Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
999 B · 26 lines
Rust
123456789101112131415161718192021222324252627use std::sync::Arc;
use bobbin_runtime::{Clock, SystemClock};use tracing_subscriber::{EnvFilter, filter::LevelFilter};use tranquil_gate::config::LogConfig;
struct ClockTimer(Arc<dyn Clock>);
impl tracing_subscriber::fmt::time::FormatTime for ClockTimer { fn format_time(&self, w: &mut tracing_subscriber::fmt::format::Writer<'_>) -> std::fmt::Result { write!(w, "{}", self.0.now_unix_micros().raw()) }}
pub fn init(config: &LogConfig) -> anyhow::Result<()> { let filter = EnvFilter::try_new(format!("{},{}", LevelFilter::INFO, config.filter))?; let subscriber = tracing_subscriber::fmt() .with_env_filter(filter) .with_timer(ClockTimer(Arc::new(SystemClock::new()))); let result = match config.format.as_str() { "text" => subscriber.try_init(), "json" => subscriber.json().try_init(), _ => anyhow::bail!("log.format must be text or json"), }; result.map_err(|error| anyhow::anyhow!("initialize tracing: {error}"))}