fix(logger): keep rendered redacted line when template scan flags a key pattern

The blanket REDACTED for a changed msg/color template discarded lines
whose rendered form was already redacted by the same pipeline, e.g.
'password=%s' became 'REDACTED' instead of 'password=REDACTED'. Only
fall back to REDACTED when the rendered form did not change either,
which is where interpolation can mangle the key pattern the scrub
would otherwise see.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
Yujong Lee 2026-09-23 00:38:37 +00:00
parent cb68636890
commit 458672db9d
2 changed files with 17 additions and 2 deletions

View file

@ -470,7 +470,7 @@ def _process_record(record: logging.LogRecord, *, base64_limit: int, text_limit:
else _python_process_diagnostic(message, exception, stack, leaves, redact, base64_limit, text_limit)
)
raw_template_changed: Final = raw_template is not None and processed_leaves[len(extra_leaves)] != raw_template
safe_message: Final = "REDACTED" if raw_template_changed else processed_message
safe_message: Final = "REDACTED" if raw_template_changed and processed_message == message else processed_message
if redact or safe_message != message:
record.msg = safe_message # rebind-ok: the Filter interface mutates the record
record.args = None # rebind-ok: the rendered message replaces interpolation inputs
@ -486,7 +486,7 @@ def _process_record(record: logging.LogRecord, *, base64_limit: int, text_limit:
raw_color_changed: Final = (
raw_color is not None and processed_leaves[len(extra_leaves) + int(raw_template is not None)] != raw_color
)
if raw_color_changed:
if raw_color_changed and getattr(record, "color_message", None) == substituted_color:
setattr(record, "color_message", "REDACTED")
setattr(record, _REDACTED_RECORD_ATTR, _REDACTED_STAMP)
return True

View file

@ -868,6 +868,21 @@ def test_malformed_interpolation_still_scrubs_a_record(monkeypatch, native):
assert record.color_message == "REDACTED"
@pytest.mark.parametrize("native", (False, True), ids=("python", "rust"))
def test_key_pattern_template_keeps_the_rendered_redacted_line(monkeypatch, native):
if native:
pytest.importorskip("litellm.rust_bridge._native")
monkeypatch.setenv("LITELLM_RUST", "1" if native else "0")
monkeypatch.setattr("litellm._logging._ENABLE_SECRET_REDACTION", True)
record = _make_record(logging.INFO, "password=%s ok", ("hunter2",))
record.color_message = "password=%s ok"
assert DiagnosticProcessingFilter().filter(record) is True
assert record.getMessage() == "REDACTED ok"
assert record.color_message == "REDACTED ok"
def test_disabled_diagnostic_call_does_not_render_arguments(caplog):
class Unrenderable:
def __str__(self):