openai_agents_rust/
tracing.rs

1use tracing::{Level, info};
2use tracing_subscriber::prelude::*;
3use tracing_subscriber::{EnvFilter, fmt};
4
5/// Initialize global tracing subscriber.
6///
7/// This function sets up a `tracing_subscriber` that respects the `RUST_LOG`
8/// environment variable (or defaults to `info`). It should be called once at
9/// application startup, typically from `main.rs`.
10pub fn init_tracing() {
11    // Build a subscriber that formats logs in a human‑readable way.
12    let fmt_layer = fmt::layer()
13        .with_target(true)
14        .with_thread_ids(true)
15        .with_thread_names(true);
16
17    // Use `EnvFilter` to enable dynamic log level control via `RUST_LOG`.
18    let filter_layer = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
19
20    // Combine layers and set as the global default.
21    tracing_subscriber::registry()
22        .with(filter_layer)
23        .with(fmt_layer)
24        .init();
25
26    info!(level = %Level::INFO, "Tracing initialized");
27}