print().
This guide contains:
- The original minimal application with
print()statements - Installation instructions
- A consolidated runnable example using OpenTelemetry logging
- Notes on processors, severity values, and example output
- Links and resources to extend this setup
Initial application (before OpenTelemetry)
This is the minimal starting point that usesprint() for diagnostic output:
print() calls with OpenTelemetry LogRecord emissions for richer, structured logging.
Installation
Install the OpenTelemetry API and SDK packages for Python:OpenTelemetry logging — components overview
Below are the main components you’ll configure when using OpenTelemetry logging:Important note about module paths
Some OpenTelemetry logging modules live under names that begin with an underscore (for example,
opentelemetry.sdk._logs). This signals an experimental or not-yet-stable API surface. If you see import errors after upgrading packages, check the library release notes or the OpenTelemetry Python releases for updated module paths.Consolidated runnable example
The example below replaces theprint() calls with LogRecord emissions and configures a Console exporter and a BatchLogRecordProcessor. Save as main.py and run python main.py to see JSON-like LogRecord entries printed to the terminal.
Notes on BatchLogRecordProcessor
- The
BatchLogRecordProcessorbuffers LogRecord objects and sends them to the exporter in batches on a schedule or when the queue fills. This improves throughput by avoiding the overhead of exporting each record synchronously. - For quick debugging or demos you can swap in a
SimpleLogRecordProcessorwhich sends each record immediately; however,BatchLogRecordProcessoris preferred for production-like workloads.
Severity text and numbers
OpenTelemetry provides aSeverityNumber enum (for example SeverityNumber.INFO) which is more readable and maintainable than hard-coding numeric severity values. Use severity_text for a human-readable level and severity_number for the numeric enum.
Example output
Running the example above prints JSON-like LogRecord entries via theConsoleLogExporter. Below is a formatted example of what one entry may look like:
attributes, resource attributes, or trace/span correlation fields to enrich your logs.
Summary & next steps
- Replace
print()statements withlogger.emit(LogRecord(...))to produce structured logs. - Configure and set a global
LoggerProvider, attach an exporter (Console used here) and a processor (BatchLogRecordProcessor). - Use
get_logger()to obtain a logger wired to your provider, and populatebody,timestamp,severity_text,severity_number, andattributesfor richer diagnostics. - Extend this minimal setup by exporting to an OTLP collector, adding service-specific resource attributes, or correlating logs with traces.
Links and references
- OpenTelemetry Python Documentation
- OpenTelemetry Python SDK on GitHub
- OpenTelemetry Collector / OTLP exporters
- Release notes and module path changes: OpenTelemetry Python Releases