Skip to main content

headless_lms_base/
tracing.rs

1use anyhow::Result;
2use tracing_error::ErrorLayer;
3use tracing_log::LogTracer;
4use tracing_subscriber::{EnvFilter, layer::SubscriberExt};
5
6/**
7Sets up tokio tracing. Also makes sure that log statements from libraries respect the log level
8settings that have been set with RUST_LOG, for example:
9
10```no_run
11use std::env;
12unsafe {
13    env::set_var("RUST_LOG", "info,actix_web=info,sqlx=warn");
14}
15```
16*/
17pub fn setup_tracing() -> Result<()> {
18    // Resolve the color decision once so the error formatter colors only on a TTY.
19    crate::error::clean_format::color::init_auto();
20
21    let subscriber = tracing_subscriber::Registry::default()
22        .with(
23            tracing_subscriber::fmt::layer()
24                .event_format(tracing_subscriber::fmt::format().compact()),
25        )
26        .with(ErrorLayer::default())
27        .with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")));
28    tracing::subscriber::set_global_default(subscriber)?;
29    LogTracer::init()?;
30    Ok(())
31}