Overview
Theopentelemetry-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 yourCargo.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 mapslog::Record fields to OpenTelemetry LogRecord fields:
Basic Fields
Severity Mapping
Metadata Attributes (Experimental)
With theexperimental_metadata_attributes feature, source code metadata is captured:
Key-Value Attributes
Thelog crate supports structured logging with key-value pairs:
Type Mapping
With Serde Support
Enable thewith-serde feature for complex types:
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:
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:trace_id: The ID of the distributed tracespan_id: The ID of the current spantrace_flags: Sampling flags
Performance Considerations
Filtering
Set appropriate log levels to avoid overhead:event_enabled
The appender implementsenabled() to check if a log should be processed:
Batching
UseBatchLogProcessor in production to amortize export costs:
- Reduces network overhead
- Improves throughput
- Adds minimal latency (configurable)
Comparison with tracing Appender
Choose the
log appender if:
- You have existing code using the
logcrate - You need simple, synchronous logging
- You don’t need advanced filtering or span correlation
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
-
Check log level: Ensure
set_max_level()is set appropriately -
Flush on shutdown: Always call
provider.shutdown() - Check processor configuration: Verify the exporter is configured correctly
Complex types not serialized
Enable thewith-serde feature:
Trace context not attached
Ensure logs are emitted within an active span context:See Also
tracing Appender
Alternative appender for the
tracing crateLog Processors
Configure batch and simple processors
Bridge API
Understanding the underlying API
Overview
Return to logs overview