summaryrefslogtreecommitdiffstats
path: root/Lib/logging
diff options
context:
space:
mode:
authorRomuald Brunet <romuald@chivil.com>2023-08-15 07:23:54 (GMT)
committerGitHub <noreply@github.com>2023-08-15 07:23:54 (GMT)
commita482e5bf0022f85424a6308529a9ad51f1bfbb71 (patch)
treef2d67ed61e2a8e005becbb1ae32b36d9e6646aa1 /Lib/logging
parent580f357c663bd704a0903e4174cdf458cac56416 (diff)
downloadcpython-a482e5bf0022f85424a6308529a9ad51f1bfbb71.zip
cpython-a482e5bf0022f85424a6308529a9ad51f1bfbb71.tar.gz
cpython-a482e5bf0022f85424a6308529a9ad51f1bfbb71.tar.bz2
gh-76913: Add "merge extras" feature to LoggerAdapter (GH-107292)
Diffstat (limited to 'Lib/logging')
-rw-r--r--Lib/logging/__init__.py18
1 files changed, 16 insertions, 2 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index 527fc5c..2d228e5 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -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
#