{"id":1066,"library":"opentelemetry-instrumentation-logging","title":"OpenTelemetry Logging Instrumentation","description":"The `opentelemetry-instrumentation-logging` library provides automatic instrumentation for Python's standard `logging` module. It converts native Python log messages into OpenTelemetry logs, enabling correlation with traces and metrics, and facilitates their export to an observability backend. Part of the `opentelemetry-python-contrib` project, this library is currently in beta (version 0.61b0) and undergoes regular releases as part of the broader OpenTelemetry Python ecosystem.","status":"active","version":"0.61b0","language":"python","source_language":"en","source_url":"https://github.com/open-telemetry/opentelemetry-python-contrib","tags":["opentelemetry","logging","instrumentation","observability","tracing","logs"],"install":[{"cmd":"pip install opentelemetry-instrumentation-logging opentelemetry-sdk opentelemetry-exporter-otlp","lang":"bash","label":"Install core and OTLP exporter"}],"dependencies":[{"reason":"Core OpenTelemetry API for defining telemetry.","package":"opentelemetry-api"},{"reason":"Core OpenTelemetry SDK for processing and exporting telemetry.","package":"opentelemetry-sdk"},{"reason":"Common exporter for sending logs via OTLP (OpenTelemetry Protocol).","package":"opentelemetry-exporter-otlp","optional":true}],"imports":[{"symbol":"LoggingInstrumentor","correct":"from opentelemetry.instrumentation.logging import LoggingInstrumentor"},{"symbol":"LoggerProvider","correct":"from opentelemetry.sdk._logs import LoggerProvider"},{"symbol":"BatchLogRecordProcessor","correct":"from opentelemetry.sdk._logs.export import BatchLogRecordProcessor"},{"symbol":"OTLPLogExporter","correct":"from opentelemetry.exporter.otlp.proto.grpc._log_exporter import OTLPLogExporter"}],"quickstart":{"code":"import logging\nfrom opentelemetry import trace\nfrom opentelemetry._logs import set_logger_provider\nfrom opentelemetry.sdk._logs import LoggerProvider\nfrom opentelemetry.sdk._logs.export import BatchLogRecordProcessor, ConsoleLogRecordExporter\nfrom opentelemetry.sdk.resources import Resource\nfrom opentelemetry.instrumentation.logging import LoggingInstrumentor\n\n# Configure a basic logger\nlogging.basicConfig(level=logging.INFO)\nlogger = logging.getLogger(__name__)\n\n# 1. Configure OpenTelemetry Resource\nresource = Resource.create({\n    \"service.name\": \"my-python-logging-app\",\n    \"service.instance.id\": \"instance-1\"\n})\n\n# 2. Configure LoggerProvider with a processor and exporter\n# For demonstration, using ConsoleLogRecordExporter to print to stdout\n# In a real application, you'd use OTLPLogExporter or another backend exporter.\nlog_exporter = ConsoleLogRecordExporter()\nlog_processor = BatchLogRecordProcessor(log_exporter)\nlogger_provider = LoggerProvider(resource=resource)\nlogger_provider.add_log_record_processor(log_processor)\nset_logger_provider(logger_provider)\n\n# 3. Instrument the standard logging module\n# set_logging_format=True enables trace context injection into log format\nLoggingInstrumentor().instrument(set_logging_format=True)\n\n# 4. Use standard logging, which will now be captured by OpenTelemetry\nlogger.info(\"This is a regular Python log message.\")\n\n# 5. Demonstrate log correlation with an active trace span\ntracer = trace.get_tracer(\"my-tracer\")\nwith tracer.start_as_current_span(\"my-operation\") as span:\n    span.set_attribute(\"operation.id\", \"xyz123\")\n    logger.warning(\"This log message is inside a span, so it should be correlated.\")\n\n# 6. Shut down the logger provider to ensure all logs are exported\nlogger_provider.shutdown()\nprint(\"Logs sent to console (or configured exporter).\")","lang":"python","description":"This quickstart demonstrates how to set up `opentelemetry-instrumentation-logging` to capture standard Python log messages and export them. It configures a `LoggerProvider` with a `ConsoleLogRecordExporter` for easy viewing, enables the `LoggingInstrumentor` to inject trace context, and shows a log message emitted within an active span for correlation."},"warnings":[{"fix":"Review release notes for each update. Pin dependencies to specific versions (`==0.x.y`) in production to control updates.","message":"The library is currently in beta (`0.x.y` versioning). While stable for many use cases, its API is subject to change, and breaking changes may occur in minor or patch releases.","severity":"breaking","affected_versions":"All 0.x.y beta versions"},{"fix":"Avoid calling `uninstrument()` if possible. If dynamic instrumentation/uninstrumentation is strictly necessary, carefully evaluate its impact on other logging components and consider alternative approaches like logging filters or custom loggers that do not rely on modifying the global log factory. Refer to GitHub Issue #3808 for context.","message":"Calling `LoggingInstrumentor().uninstrument()` can corrupt the standard Python `logging` module's log factory linked list, potentially breaking other logging handlers or custom log record factories that were set after `LoggingInstrumentor`.","severity":"gotcha","affected_versions":"All 0.x.y beta versions (at least up to 0.61b0)"},{"fix":"Ensure `LoggingInstrumentor().instrument(set_logging_format=True)` is called early in your application's startup, prior to any custom `logging.basicConfig()` calls that use trace context variables (e.g., `%(otelTraceID)s`).","message":"If you manually call `logging.basicConfig()` and are not relying on the `OTEL_PYTHON_LOG_CORRELATION` environment variable to configure the logging format, you *must* ensure `LoggingInstrumentor().instrument()` is called *before* `logging.basicConfig()` to avoid `KeyError` exceptions when attempting to inject trace context variables.","severity":"gotcha","affected_versions":"All 0.x.y beta versions"},{"fix":"Install `opentelemetry-instrumentation-logging` and call `LoggingInstrumentor().instrument()` (or rely on auto-instrumentation). Avoid using the `OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED` environment variable if you are using this package.","message":"This package provides a logging handler to replace a deprecated `LoggingHandler` previously available in `opentelemetry-sdk`. If `opentelemetry-instrumentation-logging` is installed, you should *not* set the `OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED` environment variable to `true`, as this variable is intended for the *deprecated* SDK handler and can cause confusion or conflicts.","severity":"deprecated","affected_versions":"Versions where `opentelemetry-sdk` deprecated its internal `LoggingHandler`."},{"fix":"Always follow the OpenTelemetry logging setup by creating a `Resource`, `LoggerProvider`, `LogRecordProcessor`, and `LogExporter`, and setting the global logger provider via `set_logger_provider()`.","message":"Simply installing `opentelemetry-instrumentation-logging` and calling `instrument()` is not sufficient to export logs. You must also configure a `LoggerProvider`, add a `LogRecordProcessor`, and specify a `LogExporter` (e.g., `OTLPLogExporter`) to direct your OpenTelemetry logs to an observability backend.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'0.61':57 'automat':11 'b0':58 'backend':41 'beta':55 'broader':67 'contrib':48 'convert':20 'correl':29 'current':53 'ecosystem':70 'enabl':28 'export':37 'facilit':35 'instrument':3,7,12,73 'librari':9,51 'log':2,8,17,23,27,72,76 'messag':24 'metric':33 'modul':18 'nativ':21 'observ':40,74 'opentelemetri':1,6,26,46,68,71 'opentelemetry-instrumentation-log':5 'opentelemetry-python-contrib':45 'part':42,64 'project':49 'provid':10 'python':14,22,47,69 'regular':61 'releas':62 'standard':16 'trace':31,75 'undergo':60 'version':56","created_at":"2026-04-01T15:38:19.182181+00:00","updated_at":"2026-04-16T17:42:12.069242+00:00","problems":{"verify_error":"error: Failed to parse: `opentelemetry-instrumentation-logging opentelemetry-sdk opentelemetry-exporter-otlp`\n  Caused by: Expected one of `@`, `(`, `<`, `=`, `>`, `~`, `!`, `;`, found `o`\nopentelemetry-instrumentation-logging opentelemetry-sdk opentelemetry-exporter-otlp\n                           "},"ecosystem":"pypi","meta_description":null,"install_score":100,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.63b1","cli_name":"opentelemetry-instrument","cli_version":"opentelemetry-instrument 0.62b1","type":"library","homepage":"https://opentelemetry.io","github":"https://github.com/open-telemetry/opentelemetry-python-contrib","docs":null,"changelog":null,"pypi":"https://pypi.org/project/opentelemetry-instrumentation-logging/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["observability","http-networking"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"install_fail","verified_at":"2026-06-28","last_verified":"2026-06-28","next_check":"2026-07-05","install_tag":"verified"}}