- Verbose: manually creating and emitting
LogRecordobjects via an OpenTelemetry logger. - Concise: using the Python standard library
loggingmodule and forwarding those records into OpenTelemetry.
logging.info(...) calls produce OpenTelemetry log records automatically.
At-a-glance comparison
1) Verbose: emitting OpenTelemetry LogRecord directly
This approach constructs LogRecord objects and emits them through an OpenTelemetry Logger. Itβs explicit, but repetitive: every log must include timestamp, severity, body, and other fields.
LogRecord payload, but it quickly becomes cumbersome for everyday application logging.
2) Simpler: Python standard library logging
The Python standard logging module is concise and widely used. By default it writes human-readable text to the console (or to files/handlers you configure), but those records are not OpenTelemetry LogRecords until you forward them.
3) Forward stdlib logging to OpenTelemetry
You can wire the Python stdlib logger into OpenTelemetry using a LoggingHandler provided by the OpenTelemetry SDK. The handler converts logging.LogRecord instances into OpenTelemetry LogRecords and forwards them to the configured LoggerProvider. With this in place, existing logging.info(...) calls generate structured OpenTelemetry logs automatically.
Example: forward stdlib logs to OpenTelemetry and export to the console as JSON.
- If you still want the original stdlib text output on the console in addition to OpenTelemetry JSON output, attach a standard
logging.StreamHandler()alongside theLoggingHandler. - The
LoggingHandlerwill populate fields such as severity text/number, timestamps, and code attributes (file path, function name, line number) automatically.
Forwarding the Python stdlib logger to OpenTelemetry lets existing codebases emit structured OpenTelemetry logs with minimal changes β configure the handler once and continue using
logging everywhere.Links and references
- OpenTelemetry: https://opentelemetry.io/
- Python logging documentation: https://docs.python.org/3/library/logging.html
- OpenTelemetry Python SDK: https://github.com/open-telemetry/opentelemetry-python
This setup gives you the familiarity of the Python
logging API with the structured, observable benefits of OpenTelemetry logging.