Skip to main content

Overview

The opentelemetry-appender-log crate provides a bridge between the log crate and OpenTelemetry. It implements the log::Log trait to capture logs emitted through the log crate’s macros (error!, warn!, info!, debug!, trace!) and forwards them to OpenTelemetry exporters.

Installation

Add the following to your Cargo.toml:

Quick Start

1

Create a LoggerProvider

Set up the OpenTelemetry logger provider with an exporter:
2

Install the log appender

Create and register the OpenTelemetry log bridge:
3

Emit logs

Use standard log macros:
4

Shutdown

Flush remaining logs before exit:

Complete Example

Field Mapping

The appender maps log::Record fields to OpenTelemetry LogRecord fields:

Basic Fields

Severity Mapping

Metadata Attributes (Experimental)

With the experimental_metadata_attributes feature, source code metadata is captured:
This adds attributes:

Key-Value Attributes

The log crate supports structured logging with key-value pairs:
These are converted to OpenTelemetry attributes based on their type:

Type Mapping

With Serde Support

Enable the with-serde feature for complex types:
With this feature enabled: | Type | Result | Notes | |-------------------|---------------|------------------------------------|| | Sequences | ListAny | Vectors, arrays, etc. | | Maps | Map | HashMaps, BTreeMaps, etc. | | Structs | Map | Serialized as key-value maps | | Enums | Various | Depends on variant type | | Bytes | Bytes | Raw byte arrays | | Option::None | — | Discarded | | Option::Some(T) | T | Uses inner value | | () | — | Discarded | Example:
Without with-serde, complex types are formatted using Debug:

Usage with Batch Processor

For production use, configure a batch processor:

Integration with Traces

When logs are emitted within an active OpenTelemetry span context, the trace context is automatically attached:
The emitted log will include:
  • trace_id: The ID of the distributed trace
  • span_id: The ID of the current span
  • trace_flags: Sampling flags

Performance Considerations

Filtering

Set appropriate log levels to avoid overhead:

event_enabled

The appender implements enabled() to check if a log should be processed:

Batching

Use BatchLogProcessor in production to amortize export costs:
  • Reduces network overhead
  • Improves throughput
  • Adds minimal latency (configurable)
See Log Processors for configuration details.

Comparison with tracing Appender

Choose the log appender if:
  • You have existing code using the log crate
  • You need simple, synchronous logging
  • You don’t need advanced filtering or span correlation
Choose the tracing appender if:
  • You’re building async applications
  • You want hierarchical span context
  • You need advanced filtering capabilities
  • You want automatic trace correlation

Feature Flags

Troubleshooting

Logs not appearing

  1. Check log level: Ensure set_max_level() is set appropriately
  2. Flush on shutdown: Always call provider.shutdown()
  3. Check processor configuration: Verify the exporter is configured correctly

Complex types not serialized

Enable the with-serde feature:

Trace context not attached

Ensure logs are emitted within an active span context:

See Also

tracing Appender

Alternative appender for the tracing crate

Log Processors

Configure batch and simple processors

Bridge API

Understanding the underlying API

Overview

Return to logs overview