Overview
OpenTelemetry logs provide structured logging capabilities that integrate seamlessly with traces and metrics. This guide shows how to use thetracing crate with OpenTelemetry’s log appender bridge.
Dependencies
Add these dependencies to yourCargo.toml:
Cargo.toml
Complete Example
1
Initialize the Logger Provider
Set up the logger provider with a stdout exporter and resource attributes.
2
Configure Tracing Subscriber
Set up the tracing subscriber with OpenTelemetry layer and filtering.
3
Add Console Output Layer
Optionally add a fmt layer for console output alongside OpenTelemetry.
4
Emit Log Records
Use the
tracing macros to emit structured log records.Log Levels
Thetracing crate provides standard log levels:
Structured Fields
Add structured fields to your logs for better searchability:The OpenTelemetry logs API is designed to be used as a bridge. The recommended approach is to use the
tracing crate for logging in your application and let the OpenTelemetry appender bridge handle the export.Filter Configuration
Common Filter Patterns
Advanced: Custom Log Processors
You can create custom log processors to enrich or filter logs. Here’s a simple example:logs-advanced example in the source repository.