Skip to main content

Overview

Log processors are responsible for receiving log records from loggers and forwarding them to exporters. OpenTelemetry SDK provides two built-in processors:
  • SimpleLogProcessor: Exports logs immediately (for development/debugging)
  • BatchLogProcessor: Buffers logs and exports in batches (for production)

LogProcessor Trait

All processors implement the LogProcessor trait:

SimpleLogProcessor

The SimpleLogProcessor exports log records immediately when they are emitted. It’s synchronous and suitable for debugging and testing, but not recommended for production due to performance overhead.

Characteristics

  • Synchronous export on each log emission
  • No batching or buffering
  • Immediate visibility of logs
  • Higher overhead per log record
  • Simpler error handling

Usage

Or manually:

When to Use

Use SimpleLogProcessor when:
  • Developing and debugging locally
  • Running tests that need immediate log visibility
  • Prototyping with small log volumes
  • Using simple exporters like stdout
Avoid in production because:
  • Each log blocks the emitting thread
  • High overhead for high-throughput scenarios
  • Network latency affects application performance
  • No batching efficiency

Runtime Compatibility

When using SimpleLogProcessor with different OTLP exporter clients:
Deadlock Risk: Using async exporters (e.g., with tokio::sleep) in SimpleLogProcessor can cause deadlocks if all runtime worker threads are blocked. Use BatchLogProcessor for async exporters.

BatchLogProcessor

The BatchLogProcessor buffers log records in memory and exports them in batches using a background thread. This is the recommended processor for production environments.

Characteristics

  • Asynchronous export via background thread
  • Configurable batching and scheduling
  • Reduced export overhead
  • Bounded memory usage
  • Optimal for high-throughput scenarios

Basic Usage

Configuration

Configure batch behavior using BatchConfigBuilder:

Configuration Parameters

max_export_batch_size must be less than or equal to max_queue_size.

Export Triggers

Logs are exported when:
  1. Batch size reached: When max_export_batch_size logs are buffered
  2. Scheduled delay: Every scheduled_delay milliseconds
  3. Force flush: When force_flush() is called
  4. Shutdown: When shutdown() is called

Example: Production Configuration

Runtime Compatibility

When using BatchLogProcessor with OTLP exporters:

Force Flush

Explicitly export buffered logs:
This is useful before:
  • Checkpointing application state
  • Deploying new versions
  • Handling critical errors

Shutdown

Always shutdown before application exit:
Tokio Deadlock: When using tokio’s current_thread runtime, call shutdown() from a separate thread or use spawn_blocking, as it’s a blocking call that can deadlock.

Dropped Logs

If the queue is full, logs are dropped and counted. The count is logged at shutdown:
Monitor dropped logs and adjust configuration as needed.

Custom Log Processors

You can implement custom processors for specialized behavior:
Usage:

event_enabled Optimization

The event_enabled() method allows early filtering to skip expensive logging operations:

Processor Comparison

Best Practices

Always use BatchLogProcessor for production deployments:
  • Lower overhead
  • Better performance under load
  • Configurable batching behavior
Adjust batch configuration based on your log volume:
Call shutdown() before application exit to flush remaining logs:
Check for dropped log messages in production and adjust max_queue_size if needed.
Implement event_enabled() in custom processors to skip expensive operations for filtered logs.

Troubleshooting

Logs not exported

  1. Forgot to shutdown: Always call provider.shutdown()
  2. Batch not full: Wait for scheduled_delay or call force_flush()
  3. Queue full: Increase max_queue_size or export more frequently

High latency

If using SimpleLogProcessor in production, switch to BatchLogProcessor:

Memory usage too high

Reduce max_queue_size or increase export frequency:

Logs dropped

Increase queue size or export more frequently:

See Also

log Appender

Bridge logs from the log crate

tracing Appender

Bridge logs from the tracing crate

Bridge API

Understanding the core API

Overview

Return to logs overview