Skip to content

Commit 5b375e1

Browse files
authored
chore(logging): revert remove log correlation attributes from ddtrace loggers (#15027)
Reverts #14979
1 parent 26c7507 commit 5b375e1

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

ddtrace/_logger.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ def _add_file_handler(
110110
return ddtrace_file_handler
111111

112112

113+
def set_log_formatting():
114+
# type: () -> None
115+
"""Sets the log format for the ddtrace logger."""
116+
ddtrace_logger = logging.getLogger("ddtrace")
117+
for handler in ddtrace_logger.handlers:
118+
handler.setFormatter(logging.Formatter(DD_LOG_FORMAT))
119+
120+
113121
def get_log_injection_state(raw_config: Optional[str]) -> bool:
114122
"""Returns the current log injection state."""
115123
if raw_config:

ddtrace/contrib/internal/logging/patch.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import ddtrace
77
from ddtrace import config
8+
from ddtrace._logger import set_log_formatting
89
from ddtrace.contrib.internal.trace_utils import unwrap as _u
910
from ddtrace.internal.constants import LOG_ATTR_ENV
1011
from ddtrace.internal.constants import LOG_ATTR_SERVICE
@@ -99,6 +100,13 @@ def patch():
99100
_w(logging.Logger, "makeRecord", _w_makeRecord)
100101
_w(logging.StrFormatStyle, "_format", _w_StrFormatStyle_format)
101102

103+
if config._logs_injection:
104+
# Only set the formatter is DD_LOGS_INJECTION is set to True. We do not want to modify
105+
# unstructured logs if a user has not enabled logs injection.
106+
# Also, the Datadog log format must be set after the logging module has been patched,
107+
# otherwise the formatter will raise an exception.
108+
set_log_formatting()
109+
102110

103111
def unpatch():
104112
if getattr(logging, "_datadog_patch", False):

releasenotes/notes/remove-set-log-formatting-a8b9c7d2e1f3g4h5.yaml

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/integration/test_integration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,8 +808,8 @@ def test_logging_during_tracer_init_succeeds_when_debug_logging_and_logs_injecti
808808
assert out == b"", "an empty program should generate no logs under ddtrace-run"
809809

810810
assert (
811-
b"[dd.service=ddtrace_subprocess_dir dd.env= dd.version= dd.trace_id=0 dd.span_id=0]" not in err
812-
), "stderr should not contain debug output when DD_TRACE_DEBUG is set"
811+
b"[dd.service=ddtrace_subprocess_dir dd.env= dd.version= dd.trace_id=0 dd.span_id=0]" in err
812+
), "stderr should contain debug output when DD_TRACE_DEBUG is set"
813813

814814
assert b"KeyError: 'dd.service'" not in err, "stderr should not contain any exception logs"
815815
assert (

0 commit comments

Comments
 (0)