From c4fc9a9fb573cf66ae59efc61405cbcf692aef97 Mon Sep 17 00:00:00 2001 From: Munir Abdinur Date: Thu, 23 Oct 2025 17:51:44 -0400 Subject: [PATCH] fix(logging): remove log correlation attributes from ddtrace loggers (#14979) ## Description Fixes ddtrace internal logging errors when trace-log correlation is disabled programmatically. ## Problem ddtrace internal logs were expecting correlation attributes (`dd.service`, etc.) even when log injection was disabled, causing `ValueError: Formatting field not found in record` errors. ## Solution Removed set_log_formatting to prevent mismatches between expected and actual log record attributes. When log correlation is disabled but DD_LOG_FORMAT is set, internal ddtrace logs can raise a ValueError. This also prevents issues when user configured log formatters override log formatters set by ddtrace. Since span start and finish now emit debug logs, including trace attributes in all ddtrace log records is no longer necessary. ## Testing Verified ddtrace internal logs work correctly with correlation disabled. ## Risks None - fixes existing bug. (cherry picked from commit b433817a7d1ac199ca7f9cf90e52542205143b13) --- ddtrace/_logger.py | 8 -------- ddtrace/contrib/internal/logging/patch.py | 8 -------- .../notes/remove-set-log-formatting-a8b9c7d2e1f3g4h5.yaml | 5 +++++ tests/integration/test_integration.py | 4 ++-- 4 files changed, 7 insertions(+), 18 deletions(-) create mode 100644 releasenotes/notes/remove-set-log-formatting-a8b9c7d2e1f3g4h5.yaml diff --git a/ddtrace/_logger.py b/ddtrace/_logger.py index 3a6a5c5a0f5..0bb0e51d2d1 100644 --- a/ddtrace/_logger.py +++ b/ddtrace/_logger.py @@ -110,14 +110,6 @@ def _add_file_handler( return ddtrace_file_handler -def set_log_formatting(): - # type: () -> None - """Sets the log format for the ddtrace logger.""" - ddtrace_logger = logging.getLogger("ddtrace") - for handler in ddtrace_logger.handlers: - handler.setFormatter(logging.Formatter(DD_LOG_FORMAT)) - - def get_log_injection_state(raw_config: Optional[str]) -> bool: """Returns the current log injection state.""" if raw_config: diff --git a/ddtrace/contrib/internal/logging/patch.py b/ddtrace/contrib/internal/logging/patch.py index a68f0f6e4e8..c9136760c67 100644 --- a/ddtrace/contrib/internal/logging/patch.py +++ b/ddtrace/contrib/internal/logging/patch.py @@ -5,7 +5,6 @@ import ddtrace from ddtrace import config -from ddtrace._logger import set_log_formatting from ddtrace.contrib.internal.trace_utils import unwrap as _u from ddtrace.internal.constants import LOG_ATTR_ENV from ddtrace.internal.constants import LOG_ATTR_SERVICE @@ -100,13 +99,6 @@ def patch(): _w(logging.Logger, "makeRecord", _w_makeRecord) _w(logging.StrFormatStyle, "_format", _w_StrFormatStyle_format) - if config._logs_injection: - # Only set the formatter is DD_LOGS_INJECTION is set to True. We do not want to modify - # unstructured logs if a user has not enabled logs injection. - # Also, the Datadog log format must be set after the logging module has been patched, - # otherwise the formatter will raise an exception. - set_log_formatting() - def unpatch(): if getattr(logging, "_datadog_patch", False): diff --git a/releasenotes/notes/remove-set-log-formatting-a8b9c7d2e1f3g4h5.yaml b/releasenotes/notes/remove-set-log-formatting-a8b9c7d2e1f3g4h5.yaml new file mode 100644 index 00000000000..aad4e62e8c7 --- /dev/null +++ b/releasenotes/notes/remove-set-log-formatting-a8b9c7d2e1f3g4h5.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + logging: Fixed ddtrace internal logging when trace-log correlation is disabled. + Prevents ``ValueError: Formatting field not found in record: 'dd.service'``. diff --git a/tests/integration/test_integration.py b/tests/integration/test_integration.py index 60aa25db059..94a8b95a77b 100644 --- a/tests/integration/test_integration.py +++ b/tests/integration/test_integration.py @@ -808,8 +808,8 @@ def test_logging_during_tracer_init_succeeds_when_debug_logging_and_logs_injecti assert out == b"", "an empty program should generate no logs under ddtrace-run" assert ( - b"[dd.service=ddtrace_subprocess_dir dd.env= dd.version= dd.trace_id=0 dd.span_id=0]" in err - ), "stderr should contain debug output when DD_TRACE_DEBUG is set" + b"[dd.service=ddtrace_subprocess_dir dd.env= dd.version= dd.trace_id=0 dd.span_id=0]" not in err + ), "stderr should not contain debug output when DD_TRACE_DEBUG is set" assert b"KeyError: 'dd.service'" not in err, "stderr should not contain any exception logs" assert (