opentelemetry-appender-log
Version: 0.31.0 Theopentelemetry-appender-log crate provides a log appender that bridges the log crate to OpenTelemetry’s Logs Bridge API, enabling log events to be exported via OpenTelemetry.
Installation
Feature Flags
with-serde: Support complex values as attributes without stringifying (enablesserdeserialization)experimental_metadata_attributes: Include code location metadata (file path, line number, module) as attributes
Quick Start
Basic Setup
With Key-Value Attributes
Core Types
Log appender that implements
log::Log trait and forwards log records to OpenTelemetryField Mapping
This section describes howlog records map to OpenTelemetry log records:
Body
The stringified message fromlog::Record::args() becomes the log body.
Severity
log::Level maps to OpenTelemetry severity:
log::Level | Severity Text | Severity Number |
|---|---|---|
Error | Error | 17 |
Warn | Warn | 13 |
Info | Info | 9 |
Debug | Debug | 5 |
Trace | Trace | 1 |
Target
Thelog::Record::target() becomes the OpenTelemetry InstrumentationScope name.
Attributes
Key-value pairs fromlog::Record::key_values() are converted to attributes.
Type Conversion
Without with-serde Feature
| Type | OpenTelemetry Type | Notes |
|---|---|---|
i8-i64 | AnyValue::Int | |
u8-u64 | AnyValue::Int | If value fits in i64, otherwise stringified |
i128, u128 | AnyValue::Int or String | Converted to i64 if possible, else stringified |
f32, f64 | AnyValue::Double | |
bool | AnyValue::Boolean | |
&str | AnyValue::String | |
| Other | AnyValue::String | Stringified via Debug trait |
With with-serde Feature
Enables rich type support using serde:
| Type | OpenTelemetry Type | Notes |
|---|---|---|
| Primitives | As above | Same as without serde |
Bytes | AnyValue::Bytes | Binary data |
Option::None | - | Discarded |
Option::Some | Any | Uses inner value |
| Sequences | AnyValue::ListAny | Arrays, slices, tuples |
| Maps/Structs | AnyValue::Map | Structured data |
| Enum variants | AnyValue::String or Map | Unit variants as string, others as map |
Metadata Attributes
With theexperimental_metadata_attributes feature:
Examples
Basic Logging
Structured Logging
With Custom Target
Complex Types (with serde)
Performance Considerations
- The bridge evaluates
enabled()before processing log records - Use appropriate log levels to avoid unnecessary overhead
- Consider using
BatchLogProcessorfor better performance - The
with-serdefeature adds overhead for complex types
Integration with OTLP
Differences from Direct Logging
Related Crates
- opentelemetry - Core API with Logs Bridge API
- opentelemetry-sdk - SDK with log processors
- opentelemetry-appender-tracing - Bridge for
tracingcrate - log - Logging facade for Rust
Documentation
Full API Documentation
View complete API reference on docs.rs
log Crate
Documentation for the log crate