From 9c4ab4380699ea07c2f2ed3139199a709ad0db10 Mon Sep 17 00:00:00 2001 From: Vincent Gao Date: Mon, 29 Jun 2026 09:23:18 +0200 Subject: [PATCH] fix: prevent mutation of original dict when record.msg is a dict When record.msg is used as a dict log message, the format() method assigned it by reference to message_dict. Subsequent mutations to message_dict (adding exc_info, stack_info) would leak into the caller's original dict. Fix by copying the dict immediately so the original is never modified. --- src/pythonjsonlogger/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pythonjsonlogger/core.py b/src/pythonjsonlogger/core.py index d78873a..70391e7 100644 --- a/src/pythonjsonlogger/core.py +++ b/src/pythonjsonlogger/core.py @@ -243,7 +243,7 @@ def format(self, record: logging.LogRecord) -> str: # TODO: logging.LogRecord.msg and logging.LogRecord.message in typeshed # are always type of str. We shouldn't need to override that. if isinstance(record.msg, dict): - message_dict = record.msg + message_dict = record.msg.copy() record.message = "" else: record.message = record.getMessage()