bootc_internal_utils/
tracing_util.rs

1//! Helpers related to tracing, used by main entrypoints
2
3use tracing_subscriber::prelude::*;
4
5/// Initialize tracing with the default configuration.
6pub fn initialize_tracing() {
7    // Always try to use journald subscriber if we're running as root;
8    // This ensures key messages (info, warn, error) go to the journal
9    let journald_layer = if rustix::process::getuid().is_root() {
10        tracing_journald::layer()
11            .ok()
12            .map(|layer| layer.with_filter(tracing_subscriber::filter::LevelFilter::INFO))
13    } else {
14        None
15    };
16
17    // Always add the stdout/stderr layer for RUST_LOG support
18    // This preserves the existing workflow for users
19    let format = tracing_subscriber::fmt::format()
20        .without_time()
21        .with_target(false)
22        .compact();
23
24    let fmt_layer = tracing_subscriber::fmt::layer()
25        .event_format(format)
26        .with_writer(std::io::stderr)
27        .with_filter(tracing_subscriber::EnvFilter::from_default_env());
28
29    // Build the registry with layers, handling the journald layer conditionally
30    match journald_layer {
31        Some(journald) => {
32            tracing_subscriber::registry()
33                .with(fmt_layer)
34                .with(journald)
35                .init();
36        }
37        None => {
38            tracing_subscriber::registry().with(fmt_layer).init();
39        }
40    }
41}