Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 46cf5b5

Browse files
andremmemdnetoxrmx
authoredMar 21, 2025··
Add instrumentation example to logging (#3314)
* Add instrumentation example to logging * Add example output to logging instrumentation example * Fix logging instrumentation example and output * Fixing rst syntax for logging instrumentation examples --------- Co-authored-by: Emídio Neto <9735060+emdneto@users.noreply.github.com> Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
1 parent 164259e commit 46cf5b5

File tree

1 file changed

+41
-0
lines changed
  • instrumentation/opentelemetry-instrumentation-logging/src/opentelemetry/instrumentation/logging

1 file changed

+41
-0
lines changed
 

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

+41
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,47 @@
1414

1515
# pylint: disable=empty-docstring,no-value-for-parameter,no-member,no-name-in-module
1616

17+
"""
18+
The OpenTelemetry `logging` integration automatically injects tracing context into
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`.
21+
22+
.. code-block:: python
23+
24+
import logging
25+
26+
from opentelemetry.instrumentation.logging import LoggingInstrumentor
27+
28+
LoggingInstrumentor().instrument()
29+
30+
logging.warning('OTel test')
31+
32+
When running the above example you will see the following output:
33+
34+
::
35+
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
37+
38+
The environment variable `OTEL_PYTHON_LOG_CORRELATION` must be set to `true`
39+
in order to enable trace context injection into logs by calling
40+
`logging.basicConfig()` and setting a logging format that makes use of the
41+
injected tracing variables.
42+
43+
Alternatively, `set_logging_format` argument can be set to `True` when
44+
initializing the `LoggingInstrumentor` class to achieve the same effect:
45+
46+
.. code-block:: python
47+
48+
import logging
49+
50+
from opentelemetry.instrumentation.logging import LoggingInstrumentor
51+
52+
LoggingInstrumentor().instrument(set_logging_format=True)
53+
54+
logging.warning('OTel test')
55+
56+
"""
57+
1758
import logging # pylint: disable=import-self
1859
from os import environ
1960
from typing import Collection

0 commit comments

Comments
 (0)
Please sign in to comment.