{"id":4996,"library":"opencensus-ext-logging","title":"OpenCensus Logging Integration","description":"This library provides an integration for the OpenCensus tracing framework with Python's standard `logging` module. It enriches standard log records with `traceId`, `spanId`, and `traceSampled` attributes, enabling correlation of application logs with distributed traces. The current version is 0.1.1. The OpenCensus project is largely deprecated in favor of OpenTelemetry, with official support for some related exporters (e.g., Azure Monitor) ending by September 2024.","status":"deprecated","version":"0.1.1","language":"en","source_language":"en","source_url":"https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-logging","tags":["logging","observability","tracing","opencensus","deprecated"],"install":[{"cmd":"pip install opencensus-ext-logging","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Provides the core tracing context and `config_integration` functionality.","package":"opencensus","optional":false}],"imports":[{"symbol":"config_integration","correct":"from opencensus.trace import config_integration"}],"quickstart":{"code":"import logging\nimport os\nfrom opencensus.trace import config_integration\nfrom opencensus.trace import tracer as tracer_module\nfrom opencensus.trace.exporters import PrintExporter\nfrom opencensus.trace.samplers import AlwaysOnSampler\n\n# 1. Configure the logging integration\n# This must be done *before* loggers are created for existing loggers to be affected.\nconfig_integration.trace_integrations(['logging'])\n\n# 2. Set up a basic Python logger\nlogger = logging.getLogger(__name__)\nlogger.setLevel(logging.INFO)\nhandler = logging.StreamHandler()\nformatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - TraceId: %(traceId)s - SpanId: %(spanId)s - %(message)s')\nhandler.setFormatter(formatter)\nlogger.addHandler(handler)\n\n# 3. Set up an OpenCensus tracer with an exporter (e.g., PrintExporter)\n# This will activate trace context, making trace and span IDs available for logging.\nexporter = PrintExporter()\nsampler = AlwaysOnSampler()\ntracer = tracer_module.Tracer(exporter=exporter, sampler=sampler)\n\nlogger.info(\"This log message will not have trace/span IDs yet as no span is active.\")\n\n# 4. Use the tracer to create a span to enable trace context propagation\nwith tracer.span(name='my_operation') as span:\n    logger.info(\"This log message should now have trace and span IDs.\")\n    with tracer.span(name='sub_operation'):\n        logger.warning(\"Another log message within a sub-span.\")\n\nlogger.info(\"This log message is outside the span and will not have trace/span IDs from the active span.\")","lang":"python","description":"This quickstart demonstrates how to enable the OpenCensus logging integration and how log messages are enriched with `traceId` and `spanId` when an OpenCensus trace span is active. The `PrintExporter` is used to visualize the trace information, and the standard Python logging handler outputs the enriched log records."},"warnings":[{"fix":"Migrate to OpenTelemetry using its Python SDK and relevant instrumentation. OpenTelemetry provides bridges for incremental migration.","message":"The OpenCensus project, including `opencensus-ext-logging`, has been officially deprecated in favor of OpenTelemetry. OpenCensus will no longer receive new features or security updates, and users are strongly encouraged to migrate.","severity":"breaking","affected_versions":"All versions"},{"fix":"Migrate to the Azure Monitor OpenTelemetry Distro or the Azure Monitor OpenTelemetry exporters for continued support.","message":"OpenCensus Azure Monitor exporters, which commonly leverage `opencensus-ext-logging` for log correlation, are also deprecated and will be officially unsupported by September 2024.","severity":"deprecated","affected_versions":"All versions"},{"fix":"Call `config_integration.trace_integrations(['logging'])` at the very beginning of your application startup, before any loggers you wish to instrument are instantiated.","message":"The `opencensus-ext-logging` integration only affects Python `logging` instances created *after* `config_integration.trace_integrations(['logging'])` has been called. Loggers initialized before this configuration will not be enriched.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If encountering recursive logging with `AzureLogHandler`, try initializing it with `enable_local_storage=False`. The recommended long-term solution is to migrate to OpenTelemetry.","message":"When using `opencensus-ext-logging` in conjunction with certain `logging.Handler` implementations (e.g., `AzureLogHandler` from `opencensus-ext-azure`), there have been reports of recursive logging issues, which can lead to excessive logging or application crashes.","severity":"gotcha","affected_versions":"Likely older versions, but possible with 0.1.1."}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}