mirror of
https://github.com/python/cpython.git
synced 2025-11-02 03:01:58 +00:00
gh-76913: Add "merge extras" feature to LoggerAdapter (GH-107292)
This commit is contained in:
parent
580f357c66
commit
a482e5bf00
4 changed files with 66 additions and 4 deletions
|
|
@ -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
|
||||
|
||||
#
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue