{"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":"python","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,"search_vec":"'0.1.1':43 '2024':67 'applic':34 'attribut':30 'azur':62 'correl':32 'current':40 'deprec':49,72 'distribut':37 'e.g':61 'enabl':31 'end':64 'enrich':21 'export':60 'favor':51 'framework':13 'integr':3,8 'larg':48 'librari':5 'log':2,18,23,35,68 'modul':19 'monitor':63 'observ':69 'offici':55 'opencensus':1,11,45,71 'opentelemetri':53 'project':46 'provid':6 'python':15 'record':24 'relat':59 'septemb':66 'spanid':27 'standard':17,22 'support':56 'trace':12,38,70 'traceid':26 'tracesampl':29 'version':41","created_at":"2026-04-12T16:49:36.992962+00:00","updated_at":"2026-04-16T17:34:36.881248+00:00","problems":[{"fix":"Install the package using pip: `pip install opencensus-ext-logging`","cause":"This error occurs when the `opencensus-ext-logging` package has not been installed or is not accessible in the Python environment where the code is being run.","error":"ModuleNotFoundError: No module named 'opencensus-ext-logging'"},{"fix":"Ensure `config_integration.trace_integrations(['logging'])` is called at the very beginning of your application startup, before any loggers you wish to instrument are created. \n```python\nimport logging\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# This must be done *before* loggers are created\nconfig_integration.trace_integrations(['logging'])\n\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\nwith tracer_module.Tracer(sampler=AlwaysOnSampler(), exporter=PrintExporter()) as tracer:\n    with tracer.span(name='my_span'):\n        logger.info('This log should have trace and span IDs.')\n```","cause":"The logging integration was not configured before the loggers were instantiated, meaning existing loggers were not affected.","error":"Logs not showing traceId or spanId / Logging not enriched with trace information"},{"fix":"Initialize `AzureLogHandler` with `enable_local_storage=False` to prevent this behavior. Alternatively, consider migrating to OpenTelemetry as OpenCensus is deprecated. \n```python\nimport logging\nfrom opencensus.ext.azure.log_exporter import AzureLogHandler\nfrom opencensus.trace import config_integration\n\n# Configure logging integration\nconfig_integration.trace_integrations(['logging'])\n\nlogger = logging.getLogger(__name__)\nlogger.setLevel(logging.INFO)\n\n# Initialize AzureLogHandler with enable_local_storage=False\nhandler = AzureLogHandler(connection_string='InstrumentationKey=<your-instrumentation-key>', enable_local_storage=False)\nlogger.addHandler(handler)\n\nlogger.info('Test log to Azure Monitor.')\n```","cause":"When `opencensus-ext-logging` is used with `AzureLogHandler` from `opencensus-ext-azure`, a circular logging issue can occur where failed log transmissions are retried indefinitely, leading to excessive logging or application crashes.","error":"Recursive logging issue / excessive logging with AzureLogHandler"},{"fix":"Explicitly set the logging level of your logger and/or the `AzureLogHandler` to `INFO` or `DEBUG` to ensure lower-level logs are processed. \n```python\nimport logging\nfrom opencensus.ext.azure.log_exporter import AzureLogHandler\n\nlogger = logging.getLogger(__name__)\nlogger.setLevel(logging.INFO) # Set the logger level to INFO or DEBUG\n\nhandler = AzureLogHandler(connection_string='InstrumentationKey=<your-instrumentation-key>')\nhandler.setLevel(logging.INFO) # Optionally, set the handler level\nlogger.addHandler(handler)\n\nlogger.debug('This is a debug message.')\nlogger.info('This is an info message.')\nlogger.warning('This is a warning message.')\n```","cause":"The Python `logging` module's default root logger level or the `AzureLogHandler`'s level might be set to `WARNING` or higher, filtering out `INFO` or `DEBUG` level messages before they are processed by the exporter.","error":"Info logs not appearing in Application Insights / Missing lower level logs"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.1.1","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/census-instrumentation/opencensus-python","docs":null,"changelog":null,"pypi":"https://pypi.org/project/opencensus-ext-logging/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["observability","aws","gcp","azure"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-30","next_check":"2026-07-28","install_tag":null}}