Skip to content

Commit c6c59e3

Browse files
committed
Fix logging instrumentation example and output
1 parent e15fe99 commit c6c59e3

File tree

1 file changed

+24
-4
lines changed
  • instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging

1 file changed

+24
-4
lines changed

instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging/__init__.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616

1717
"""
1818
The OpenTelemetry `logging` integration automatically injects tracing context into
19-
log statements.
19+
log statements, though it is opt-in and must be enabled explicitly by setting the
20+
environment variable `OTEL_PYTHON_LOG_CORRELATION` to `true`.
2021
21-
Usage
22-
-----
2322
.. code-block:: python
2423
24+
# export OTEL_PYTHON_LOG_CORRELATION=true
25+
2526
import logging
2627
2728
from opentelemetry.instrumentation.logging import LoggingInstrumentor
@@ -32,8 +33,27 @@
3233
3334
When running the above example you will see the following output:
3435
```
35-
WARNING:root:OTel test
36+
2025-03-05 09:40:04,398 WARNING [root] [example.py:7] [trace_id=0 span_id=0 resource.service.name= trace_sampled=False] - OTel test
3637
```
38+
39+
The environment variable `OTEL_PYTHON_LOG_CORRELATION` must be set to `true`
40+
in order to enable trace context injection into logs by calling
41+
`logging.basicConfig()` and setting a logging format that makes use of the
42+
injected tracing variables.
43+
44+
Alternatively, `set_logging_format` argument can be set to `True` when
45+
initializing the `LoggingInstrumentor` class to achieve the same effect:
46+
47+
.. code-block:: python
48+
49+
import logging
50+
51+
from opentelemetry.instrumentation.logging import LoggingInstrumentor
52+
53+
LoggingInstrumentor().instrument(set_logging_format=True)
54+
55+
logging.warning('OTel test')
56+
3757
"""
3858

3959
import logging # pylint: disable=import-self

0 commit comments

Comments
 (0)