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

otel_logger::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 otel_logger::{LogFormat, Logger};

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

Logger::default() uses instrumentation scope otel_logger, compact output, ANSI enabled, INFO, console output enabled, no file appender, and OpenTelemetry log export disabled. Sampling and metric intervals use SDK environment settings or SDK defaults (parentbased_always_on and 60 seconds). 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_name (tracer instrumentation scope)orders-api
LOG_FORMATformatcompact, pretty, or json
LOG_SPAN_EVENTSspan_events`FMT::NEW
LOG_ANSIansifalse
LOG_LEVELlevelinfo
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.

Sampling uses only native OTEL_TRACES_SAMPLER / OTEL_TRACES_SAMPLER_ARG, or the SDK default parentbased_always_on. Metrics use OTEL_METRIC_EXPORT_INTERVAL in milliseconds, defaulting to 60 seconds. Neither setting has a Logger field or LOG_* fallback. Logger::service_name and LOG_SERVICE_NAME only configure the tracer’s instrumentation scope. Resource service.name uses SDK defaults; set OTEL_SERVICE_NAME or provide explicit resource attributes to configure it. These native settings also work without the env feature. See precedence details.