Skip to main content

Crate otel_logger

Crate otel_logger 

Source
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.
LoggerFileAppender
Configuration for file-based log appender.
LoggerGuard
Owns the OpenTelemetry providers and non-blocking log writers initialized by super::Logger.
OtelGuard
A guard that holds the tracer provider, meter provider, and logger provider and ensures proper cleanup.

Enums§

LogFormat
Log output format
LogRollingRotation
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.

Type Aliases§

BoxLayer