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
Complete Example
Field Mapping
The appender mapslog::Record fields to OpenTelemetry LogRecord fields:
Basic Fields
| log Field | OpenTelemetry Field | Notes |
|---|---|---|
args() | body | The formatted log message |
level() | severity_number | Mapped using severity table below |
level() | severity_text | String representation (“ERROR”, “INFO”) |
target() | target | Module/component identifier |
key_values() | attributes | Structured key-value pairs |
Severity Mapping
| log::Level | Severity Text | Severity Number |
|---|---|---|
Error | ERROR | 17 |
Warn | WARN | 13 |
Info | INFO | 9 |
Debug | DEBUG | 5 |
Trace | TRACE | 1 |
Metadata Attributes (Experimental)
With theexperimental_metadata_attributes feature, source code metadata is captured:
| log Field | Attribute Key | Example Value |
|---|---|---|
file() | code.filepath | src/main.rs |
line() | code.lineno | 42 |
module_path() | code.namespace | my_app::module |
Key-Value Attributes
Thelog crate supports structured logging with key-value pairs:
Type Mapping
| Rust Type | AnyValue Type | Notes |
|---|---|---|
i8-i64 | Int | |
u8-u64 | Int | Converted to i64 if possible, else String |
i128, u128 | Int or String | Uses Int if fits in i64, else stringified |
f32, f64 | Double | |
bool | Boolean | |
&str, String | String | |
| Other types | String | Formatted using Debug |
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
| Feature | log Appender | tracing Appender |
|---|---|---|
| Target Crate | log | tracing |
| Structured Logging | Key-values | Fields + spans |
| Span Context | Manual | Automatic |
| Async-aware | No | Yes |
| Filtering | Level-based | Target + level-based |
| Event Names | Not supported | Supported |
| Best For | Simple applications | Async applications |
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
| Feature | Description |
|---|---|
with-serde | Support complex types via serde serialization |
experimental_metadata_attributes | Capture source code location as attributes |
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