Skip to content

Commit

Permalink
bpo-31457: Don't omit inner process() calls with nested LogAdapte…
Browse files Browse the repository at this point in the history
…rs (python#4044)

This used to be the case on Python 2.  Commit
212b590 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.
  • Loading branch information
ambv authored Oct 19, 2017
1 parent 05a634b commit ce9e625
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
6 changes: 5 additions & 1 deletion Lib/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1713,7 +1713,7 @@ def log(self, level, msg, *args, **kwargs):
"""
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):
"""
Expand Down Expand Up @@ -1760,6 +1760,10 @@ def manager(self):
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())
Expand Down
20 changes: 14 additions & 6 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3904,7 +3904,6 @@ def test_critical(self):


class LoggerAdapterTest(unittest.TestCase):

def setUp(self):
super(LoggerAdapterTest, self).setUp()
old_handler_list = logging._handlerList[:]
Expand Down Expand Up @@ -3979,27 +3978,36 @@ def test_has_handlers(self):
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)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
If nested log adapters are used, the inner ``process()`` methods are no
longer omitted.

0 comments on commit ce9e625

Please sign in to comment.