gh-138162: Fix logging.LoggerAdapter with merge_extra=True and without the extra argument (GH-140511)

This commit is contained in:
Serhiy Storchaka 2025-10-30 12:52:02 +02:00 committed by GitHub
parent 622d97b8bb
commit 327dbbedff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 50 additions and 11 deletions

View file

@ -1849,9 +1849,9 @@ class LoggerAdapter(object):
def __init__(self, logger, extra=None, merge_extra=False):
"""
Initialize the adapter with a logger and a dict-like object which
provides contextual information. This constructor signature allows
easy stacking of LoggerAdapters, if so desired.
Initialize the adapter with a logger and an optional dict-like object
which provides contextual information. This constructor signature
allows easy stacking of LoggerAdapters, if so desired.
You can effectively pass keyword arguments as shown in the
following example:
@ -1882,8 +1882,9 @@ def process(self, msg, kwargs):
Normally, you'll only need to override this one method in a
LoggerAdapter subclass for your specific needs.
"""
if self.merge_extra and "extra" in kwargs:
kwargs["extra"] = {**self.extra, **kwargs["extra"]}
if self.merge_extra and kwargs.get("extra") is not None:
if self.extra is not None:
kwargs["extra"] = {**self.extra, **kwargs["extra"]}
else:
kwargs["extra"] = self.extra
return msg, kwargs