Skip to main content

tecton_core/
logging.rs

1//! Tracing/logging bootstrap helpers.
2
3use crate::error::{Result, TectonError};
4use tracing_subscriber::{fmt, prelude::*, EnvFilter};
5
6/// Initialize structured logging with an env-filter.
7///
8/// Respects `RUST_LOG` when set; otherwise uses `default_level`.
9pub fn init_tracing(default_level: &str) -> Result<()> {
10    let filter = EnvFilter::try_from_default_env()
11        .or_else(|_| EnvFilter::try_new(default_level))
12        .map_err(|e| TectonError::config(format!("invalid log filter: {e}")))?;
13
14    // Avoid double-init when CLI and UI share a process.
15    if tracing::dispatcher::has_been_set() {
16        return Ok(());
17    }
18
19    tracing_subscriber::registry()
20        .with(filter)
21        .with(
22            fmt::layer()
23                .with_target(true)
24                .with_thread_ids(false)
25                .compact(),
26        )
27        .try_init()
28        .map_err(|e| TectonError::Internal(format!("failed to init tracing: {e}")))?;
29
30    Ok(())
31}