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.
pip install opencensus-ext-loggingVerified import paths — ran on the pinned version, not inferred.
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.
Migrate to OpenTelemetry using its Python SDK and relevant instrumentation. OpenTelemetry provides bridges for incremental migration.
Migrate to the Azure Monitor OpenTelemetry Distro or the Azure Monitor OpenTelemetry exporters for continued support.
Call `config_integration.trace_integrations(['logging'])` at the very beginning of your application startup, before any loggers you wish to instrument are instantiated.
If encountering recursive logging with `AzureLogHandler`, try initializing it with `enable_local_storage=False`. The recommended long-term solution is to migrate to OpenTelemetry.
Install the package using pip: `pip install opencensus-ext-logging`
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.
```python
import logging
from opencensus.trace import config_integration
from opencensus.trace import tracer as tracer_module
from opencensus.trace.exporters import PrintExporter
from opencensus.trace.samplers import AlwaysOnSampler
# This must be done *before* loggers are created
config_integration.trace_integrations(['logging'])
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - TraceId: %(traceId)s - SpanId: %(spanId)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
with tracer_module.Tracer(sampler=AlwaysOnSampler(), exporter=PrintExporter()) as tracer:
with tracer.span(name='my_span'):
logger.info('This log should have trace and span IDs.')
```Initialize `AzureLogHandler` with `enable_local_storage=False` to prevent this behavior. Alternatively, consider migrating to OpenTelemetry as OpenCensus is deprecated.
```python
import logging
from opencensus.ext.azure.log_exporter import AzureLogHandler
from opencensus.trace import config_integration
# Configure logging integration
config_integration.trace_integrations(['logging'])
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# Initialize AzureLogHandler with enable_local_storage=False
handler = AzureLogHandler(connection_string='InstrumentationKey=<your-instrumentation-key>', enable_local_storage=False)
logger.addHandler(handler)
logger.info('Test log to Azure Monitor.')
```Explicitly set the logging level of your logger and/or the `AzureLogHandler` to `INFO` or `DEBUG` to ensure lower-level logs are processed.
```python
import logging
from opencensus.ext.azure.log_exporter import AzureLogHandler
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO) # Set the logger level to INFO or DEBUG
handler = AzureLogHandler(connection_string='InstrumentationKey=<your-instrumentation-key>')
handler.setLevel(logging.INFO) # Optionally, set the handler level
logger.addHandler(handler)
logger.debug('This is a debug message.')
logger.info('This is an info message.')
logger.warning('This is a warning message.')
```