monorail/app/
mod.rs

1pub(crate) mod analyze;
2pub(crate) mod checkpoint;
3pub(crate) mod config;
4pub(crate) mod log;
5pub(crate) mod out;
6pub(crate) mod result;
7pub(crate) mod run;
8pub(crate) mod target;
9
10use std::result::Result;
11
12use tracing_subscriber::filter::{EnvFilter, LevelFilter};
13use tracing_subscriber::layer::SubscriberExt;
14use tracing_subscriber::{fmt, Registry};
15
16use crate::core::error::MonorailError;
17
18// Custom formatter to match chrono's strict RFC3339 compliance.
19struct UtcTimestampWithOffset;
20
21impl fmt::time::FormatTime for UtcTimestampWithOffset {
22    fn format_time(&self, w: &mut fmt::format::Writer<'_>) -> std::fmt::Result {
23        let now = chrono::Utc::now();
24        // Format the timestamp as "YYYY-MM-DDTHH:MM:SS.f+00:00"
25        write!(w, "{}", now.format("%Y-%m-%dT%H:%M:%S%.f+00:00"))
26    }
27}
28
29pub(crate) fn setup_tracing(format: &str, level: u8) -> Result<(), MonorailError> {
30    let env_filter = EnvFilter::default();
31    let level_filter = match level {
32        0 => LevelFilter::OFF,
33        1 => LevelFilter::INFO,
34        2 => LevelFilter::DEBUG,
35        _ => LevelFilter::TRACE,
36    };
37
38    let registry = Registry::default().with(env_filter.add_directive(level_filter.into()));
39    match format {
40        "json" => {
41            let json_fmt_layer = tracing_subscriber::fmt::layer()
42                .json()
43                .with_current_span(false)
44                .with_span_list(false)
45                .with_target(false)
46                .flatten_event(true)
47                .with_timer(UtcTimestampWithOffset);
48            tracing::subscriber::set_global_default(registry.with(json_fmt_layer))
49                .map_err(|e| MonorailError::from(e.to_string()))?;
50        }
51        _ => {
52            let plain_fmt_layer = tracing_subscriber::fmt::layer()
53                .with_target(true)
54                .with_thread_names(true)
55                .with_timer(UtcTimestampWithOffset);
56            tracing::subscriber::set_global_default(registry.with(plain_fmt_layer))
57                .map_err(|e| MonorailError::from(e.to_string()))?;
58        }
59    }
60    Ok(())
61}