Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-76913: Add "merge extras" feature to LoggerAdapter #107292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Doc/library/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -984,10 +984,14 @@ LoggerAdapter Objects
information into logging calls. For a usage example, see the section on
:ref:`adding contextual information to your logging output <context-info>`.

.. class:: LoggerAdapter(logger, extra)
.. class:: LoggerAdapter(logger, extra, merge_extra=False)

Returns an instance of :class:`LoggerAdapter` initialized with an
underlying :class:`Logger` instance and a dict-like object.
underlying :class:`Logger` instance, a dict-like object (*extra*), and a
boolean (*merge_extra*) indicating whether or not the *extra* argument of
individual log calls should be merged with the :class:`LoggerAdapter` extra.
The default behavior is to ignore the *extra* argument of individual log
calls and only use the one of the :class:`LoggerAdapter` instance

.. method:: process(msg, kwargs)

Expand Down Expand Up @@ -1019,6 +1023,9 @@ interchangeably.
Remove the undocumented ``warn()`` method which was an alias to the
``warning()`` method.

.. versionchanged:: 3.13
The *merge_extra* argument was added.


Thread Safety
-------------
Expand Down
18 changes: 16 additions & 2 deletions Lib/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1880,7 +1880,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
Expand All @@ -1890,9 +1890,20 @@ def __init__(self, logger, extra=None):
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):
"""
Expand All @@ -1904,7 +1915,10 @@ def process(self, msg, kwargs):
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

#
Expand Down
40 changes: 40 additions & 0 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -5433,6 +5433,46 @@ def process(self, msg, kwargs):
self.assertIs(adapter.manager, orig_manager)
self.assertIs(self.logger.manager, orig_manager)

def test_extra_in_records(self):
self.adapter = logging.LoggerAdapter(logger=self.logger,
extra={'foo': '1'})

self.adapter.critical('foo should be here')
self.assertEqual(len(self.recording.records), 1)
record = self.recording.records[0]
self.assertTrue(hasattr(record, 'foo'))
self.assertEqual(record.foo, '1')

def test_extra_not_merged_by_default(self):
self.adapter.critical('foo should NOT be here', extra={'foo': 'nope'})
self.assertEqual(len(self.recording.records), 1)
record = self.recording.records[0]
self.assertFalse(hasattr(record, 'foo'))

def test_extra_merged(self):
self.adapter = logging.LoggerAdapter(logger=self.logger,
extra={'foo': '1'},
merge_extra=True)

self.adapter.critical('foo and bar should be here', extra={'bar': '2'})
self.assertEqual(len(self.recording.records), 1)
record = self.recording.records[0]
self.assertTrue(hasattr(record, 'foo'))
self.assertTrue(hasattr(record, 'bar'))
self.assertEqual(record.foo, '1')
self.assertEqual(record.bar, '2')

def test_extra_merged_log_call_has_precedence(self):
self.adapter = logging.LoggerAdapter(logger=self.logger,
extra={'foo': '1'},
merge_extra=True)

self.adapter.critical('foo shall be min', extra={'foo': '2'})
self.assertEqual(len(self.recording.records), 1)
record = self.recording.records[0]
self.assertTrue(hasattr(record, 'foo'))
self.assertEqual(record.foo, '2')


class LoggerTest(BaseTest, AssertErrorMessage):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add *merge_extra* parameter/feature to :class:`logging.LoggerAdapter`