Skip to main content
Earlier we configured Python logging and wired the standard library logger to forward records to OpenTelemetry. That initial setup correctly emitted log records, but the records did not include any trace or span identifiers because tracing had not been configured. Below is the original logging setup used for the demo:
Why trace IDs were missing
  • OpenTelemetry can automatically attach trace_id and span_id to log records, but only when a tracer is configured and a span is active.
  • With no tracer, logs still include other metadata (file, function, line) but will show zeroed or empty trace/span identifiers.
Example log record produced before tracing was configured (note the zeroed trace_id and span_id):
Complete corrected setup To automatically correlate logs with traces, install and configure both a logging provider and a tracing provider. First import the required OpenTelemetry modules:
Next configure the logging pipeline and a simple tracing pipeline that export to the console. This ensures spans and log records are printed and that logs created while a span is active will include the span and trace identifiers.
Note: with the tracer in place, any log records emitted while a span is active will include that span’s trace_id and span_id automatically.
The tracer.start_as_current_span method can be used either as a context manager or as a decorator. When used as a decorator, the decorated function runs with the new span as the current active span for the duration of the function.
Wrap logical operations with spans Update your functions so each logical operation runs inside a span. When logging occurs inside those spans, the log records will have the associated trace_id and span_id, enabling automatic correlation between logs and traces.
What you will see on the console
  • Spans printed by the ConsoleSpanExporter include trace_id and span_id.
  • Log records printed by the ConsoleLogExporter include the same trace_id and span_id when generated while the span is active.
Example span output (representative/truncated):
Corresponding log record for the “Book Flight” operation (now containing non-zero trace/span IDs):
“Reserve Flight” span and matching log record (same trace_id, unique span_id):
Why this helps
  • Automatic log-trace correlation makes root-cause analysis much faster: you can jump from a log entry directly to the full trace to see timing, parent/child spans, and contextual attributes.
  • This approach requires minimal changes: set up a tracer and ensure operations run inside spans.
Run the script Add a standard entry point and run the program to see correlated spans and logs on the console:
Summary table — components used Further reading and references This setup demonstrates how to attach trace and span identifiers to log records automatically by configuring both OpenTelemetry logging and tracing. Once enabled, logs produced while spans are active will include trace_id and span_id, giving you fast, reliable links from logs to traces for debugging and observability.

Watch Video