mirror of
https://github.com/python/cpython.git
synced 2024-12-18 06:14:00 +08:00
bpo-31457: Don't omit inner `process()
` calls with nested LogAdapters (#4044)
This used to be the case on Python 2. Commit
212b590e11
changed the implementation for Python
3, making the `log()` method of LogAdapter call `logger._log()` directly. This
makes nested log adapters not execute their ``process()`` method. This patch
fixes the issue.
Also, now proxying `name`, too, to make `repr()` work with nested log adapters.
New tests added.
This commit is contained in:
parent
05a634b12a
commit
ce9e625445
@ -1713,7 +1713,7 @@ class LoggerAdapter(object):
|
||||
"""
|
||||
if self.isEnabledFor(level):
|
||||
msg, kwargs = self.process(msg, kwargs)
|
||||
self.logger._log(level, msg, args, **kwargs)
|
||||
self.logger.log(level, msg, *args, **kwargs)
|
||||
|
||||
def isEnabledFor(self, level):
|
||||
"""
|
||||
@ -1760,6 +1760,10 @@ class LoggerAdapter(object):
|
||||
def manager(self, value):
|
||||
self.logger.manager = value
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self.logger.name
|
||||
|
||||
def __repr__(self):
|
||||
logger = self.logger
|
||||
level = getLevelName(logger.getEffectiveLevel())
|
||||
|
@ -3904,7 +3904,6 @@ class BasicConfigTest(unittest.TestCase):
|
||||
|
||||
|
||||
class LoggerAdapterTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(LoggerAdapterTest, self).setUp()
|
||||
old_handler_list = logging._handlerList[:]
|
||||
@ -3979,27 +3978,36 @@ class LoggerAdapterTest(unittest.TestCase):
|
||||
self.assertFalse(self.adapter.hasHandlers())
|
||||
|
||||
def test_nested(self):
|
||||
class Adapter(logging.LoggerAdapter):
|
||||
prefix = 'Adapter'
|
||||
|
||||
def process(self, msg, kwargs):
|
||||
return f"{self.prefix} {msg}", kwargs
|
||||
|
||||
msg = 'Adapters can be nested, yo.'
|
||||
adapter_adapter = logging.LoggerAdapter(logger=self.adapter, extra=None)
|
||||
adapter = Adapter(logger=self.logger, extra=None)
|
||||
adapter_adapter = Adapter(logger=adapter, extra=None)
|
||||
adapter_adapter.prefix = 'AdapterAdapter'
|
||||
self.assertEqual(repr(adapter), repr(adapter_adapter))
|
||||
adapter_adapter.log(logging.CRITICAL, msg, self.recording)
|
||||
self.assertEqual(len(self.recording.records), 1)
|
||||
record = self.recording.records[0]
|
||||
self.assertEqual(record.levelno, logging.CRITICAL)
|
||||
self.assertEqual(record.msg, msg)
|
||||
self.assertEqual(record.msg, f"Adapter AdapterAdapter {msg}")
|
||||
self.assertEqual(record.args, (self.recording,))
|
||||
orig_manager = adapter_adapter.manager
|
||||
self.assertIs(self.adapter.manager, orig_manager)
|
||||
self.assertIs(adapter.manager, orig_manager)
|
||||
self.assertIs(self.logger.manager, orig_manager)
|
||||
temp_manager = object()
|
||||
try:
|
||||
adapter_adapter.manager = temp_manager
|
||||
self.assertIs(adapter_adapter.manager, temp_manager)
|
||||
self.assertIs(self.adapter.manager, temp_manager)
|
||||
self.assertIs(adapter.manager, temp_manager)
|
||||
self.assertIs(self.logger.manager, temp_manager)
|
||||
finally:
|
||||
adapter_adapter.manager = orig_manager
|
||||
self.assertIs(adapter_adapter.manager, orig_manager)
|
||||
self.assertIs(self.adapter.manager, orig_manager)
|
||||
self.assertIs(adapter.manager, orig_manager)
|
||||
self.assertIs(self.logger.manager, orig_manager)
|
||||
|
||||
|
||||
|
@ -0,0 +1,2 @@
|
||||
If nested log adapters are used, the inner ``process()`` methods are no
|
||||
longer omitted.
|
Loading…
Reference in New Issue
Block a user