Skip to main content

random_image_server/
logging.rs

1use anyhow::{Result, anyhow};
2use tracing::Level;
3use tracing_subscriber::fmt::format::FmtSpan;
4
5/// Initialize the global tracing subscriber based on configuration
6///
7/// # Errors
8/// Returns an error if the subscriber cannot be initialized.
9pub fn init_logging(level: Level) -> Result<()> {
10    // Simple stdout-only logging using tracing-subscriber
11    tracing_subscriber::fmt()
12        .with_max_level(level)
13        .with_span_events(FmtSpan::NONE)
14        .with_target(true)
15        .with_thread_ids(false)
16        .with_thread_names(false)
17        .with_file(true)
18        .with_line_number(true)
19        .try_init()
20        .map_err(|e| anyhow!("Failed to initialize tracing subscriber: {e}"))?;
21
22    tracing::info!("Logging initialized: level={level:?}");
23
24    Ok(())
25}