Expand description
OpenTelemetry logging configuration and initialization.
This crate provides a flexible and configurable logging system that integrates OpenTelemetry tracing and metrics. It offers both programmatic configuration through a builder pattern and environment variable-based configuration.
§Features
- Builder-style configuration API
- Environment variable support (with “env” feature)
- Multiple log formats (compact, pretty, json)
- SDK environment settings for sampling (
OTEL_TRACES_SAMPLER) and metric export intervals (OTEL_METRIC_EXPORT_INTERVAL) - Custom resource attributes
- Optional console output
- Optional file output
The default instrumentation scope name is otel_logger.
The Logger service name identifies the tracer’s instrumentation scope.
Resource service.name uses SDK defaults or explicit resource attributes.
Set OTEL_SERVICE_NAME to configure the resource service name.
§Quick Start
use otel_logger::Logger;
use tracing::Level;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Basic initialization with console output
let guard = Logger::new("my-service").init()?;
// Your application code here...
// The guard will automatically clean up when dropped
Ok(())
}§Advanced Configuration
use otel_logger::Logger;
use tracing::Level;
use opentelemetry::KeyValue;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let guard = Logger::new("my-service")
.with_level(Level::DEBUG)
.with_console_enabled(true)
.with_attributes(vec![
KeyValue::new("environment", "production"),
KeyValue::new("version", "1.0.0"),
])
.init()?;
// Your application code here...
Ok(())
}Modules§
- default
- Default values for Logger configuration
Structs§
- FmtSpan
- Configures what points in the span lifecycle are logged as events.
- Logger
- Configuration for the OpenTelemetry tracing and logging system.
- Logger
File Appender - Configuration for file-based log appender.
- Logger
Guard - Owns the OpenTelemetry providers and non-blocking log writers initialized by
super::Logger. - Otel
Guard - A guard that holds the tracer provider, meter provider, and logger provider and ensures proper cleanup.
Enums§
- LogFormat
- Log output format
- LogRolling
Rotation - Log file rotation strategy
Functions§
- init_
env_ filter - Creates an environment filter for tracing based on the given level.
- init_
layer - Initialize a format layer with the given writer and format
- init_
logging - Initialize tracing with default settings and the given instrumentation scope name.
- init_
tracing_ from_ logger - Initialize tracing from a Logger configuration
- setup_
tracing - Initializes the complete tracing stack with OpenTelemetry integration.
The service name identifies the tracer’s instrumentation scope; resource
attributes use SDK defaults merged with
attributes.