gh-76913: Add "merge extras" feature to LoggerAdapter (GH-107292)

This commit is contained in:
Romuald Brunet 2023-08-15 09:23:54 +02:00 committed by GitHub
parent 580f357c66
commit a482e5bf00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 4 deletions

View file

@ -1879,7 +1879,7 @@ class LoggerAdapter(object):
information in logging output.
"""
def __init__(self, logger, extra=None):
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
@ -1889,9 +1889,20 @@ class LoggerAdapter(object):
following example:
adapter = LoggerAdapter(someLogger, dict(p1=v1, p2="v2"))
By default, LoggerAdapter objects will drop the "extra" argument
passed on the individual log calls to use its own instead.
Initializing it with merge_extra=True will instead merge both
maps when logging, the individual call extra taking precedence
over the LoggerAdapter instance extra
.. versionchanged:: 3.13
The *merge_extra* argument was added.
"""
self.logger = logger
self.extra = extra
self.merge_extra = merge_extra
def process(self, msg, kwargs):
"""
@ -1903,7 +1914,10 @@ class LoggerAdapter(object):
Normally, you'll only need to override this one method in a
LoggerAdapter subclass for your specific needs.
"""
kwargs["extra"] = self.extra
if self.merge_extra and "extra" in kwargs:
kwargs["extra"] = {**self.extra, **kwargs["extra"]}
else:
kwargs["extra"] = self.extra
return msg, kwargs
#