Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Logger configuration

tracing_otel::Logger is the opinionated application bootstrap. It creates the configured console and file layers, initializes trace and metric providers, optionally initializes an OpenTelemetry log provider, and installs the global tracing subscriber.

Builder configuration

#![allow(unused)]
fn main() {
use tracing::Level;
use tracing_otel::{LogFormat, Logger};

fn run() -> anyhow::Result<()> {
let _guard = Logger::new("payments-api")
    .with_format(LogFormat::Json)
    .with_level(Level::INFO)
    .with_ansi(false)
    .with_sample_ratio(0.25)
    .with_metrics_interval_secs(30)
    .with_console_enabled(true)
    .init()?;
Ok(())
}
}

Logger::default() uses compact output, ANSI enabled, INFO, full trace sampling, a 30-second metric interval, console output enabled, no file appender, and OpenTelemetry log export disabled. Environment deserialization uses false for an omitted LOG_ANSI, so set it explicitly when switching between builder and environment configuration. RUST_LOG, when valid, takes precedence over the logger level because subscriber setup reads the standard environment filter first.

Environment mapping

Logger::from_env(Some("LOG")) uses LOG as the prefix. Passing None also defaults to LOG.

Environment variableLogger fieldExample
LOG_SERVICE_NAMEservice_nameorders-api
LOG_FORMATformatcompact, pretty, or json
LOG_SPAN_EVENTSspan_events`FMT::NEW
LOG_ANSIansifalse
LOG_LEVELlevelinfo
LOG_SAMPLE_RATIOsample_ratio0.1
LOG_METRICS_INTERVAL_SECSmetrics_interval_secs30
LOG_ATTRIBUTESresource attributesdeployment.environment=prod
LOG_CONSOLE_ENABLEDconsole layertrue
LOG_OTEL_LOGS_ENABLEDOTLP log provider/layertrue

File settings use the LOG_FILE prefix:

Environment variableMeaning
LOG_FILE_ENABLEEnable the file layer
LOG_FILE_NON_BLOCKINGUse the non-blocking writer
LOG_FILE_LEVELOptional file-specific level
LOG_FILE_ANSIANSI in file output
LOG_FILE_FORMATOptional file-specific format
LOG_FILE_ROTATIONminutely, hourly, daily, or never
LOG_FILE_DIROutput directory
LOG_FILE_FILENAME_PREFIXFilename prefix
LOG_FILE_FILENAME_SUFFIXFilename suffix
LOG_FILE_MAX_LOG_FILESMaximum retained files

Environment-based configuration requires the env feature. Calling Logger::new(...) does not read LOG_*; use Logger::from_env(...) or init_logging_from_env(...) when those variables should apply.