Skip to main content

Overview

OpenTelemetry logs provide structured logging capabilities that integrate seamlessly with traces and metrics. This guide shows how to use the tracing crate with OpenTelemetry’s log appender bridge.

Dependencies

Add these dependencies to your Cargo.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

The tracing 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

Use EnvFilter to control which logs are processed. This is crucial for preventing telemetry loops and reducing noise from third-party libraries.

Common Filter Patterns

Advanced: Custom Log Processors

You can create custom log processors to enrich or filter logs. Here’s a simple example:
For a complete implementation, see the logs-advanced example in the source repository.

Output Format

The stdout exporter will output logs in JSON format: