diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2017-09-14 17:10:25 (GMT) |
---|---|---|
committer | Ćukasz Langa <lukasz@langa.pl> | 2017-09-14 17:10:25 (GMT) |
commit | 20fa05d0223101b8e0005525b94a2eac892348de (patch) | |
tree | b1f1e55f86d627674fc1dc8b40a8ddb62658e120 | |
parent | c8b6506404540eb06c45a51b04ccc102f8b92813 (diff) | |
download | cpython-20fa05d0223101b8e0005525b94a2eac892348de.zip cpython-20fa05d0223101b8e0005525b94a2eac892348de.tar.gz cpython-20fa05d0223101b8e0005525b94a2eac892348de.tar.bz2 |
[3.6] bpo-31457: Allow for nested LoggerAdapter objects (GH-3551) (#3576)
Some of the proxied methods use internal Logger state which isn't proxied,
causing failures if an adapter is applied to another adapter.
This commit fixes the issue, adds a new test for the use case.
(cherry picked from commit 1bbd482bcf6ea36bfe488f868810ffe110238ae1)
-rw-r--r-- | Lib/logging/__init__.py | 21 | ||||
-rw-r--r-- | Lib/test/test_logging.py | 11 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2017-09-13-13-33-39.bpo-31457.bIVBtI.rst | 1 |
3 files changed, 33 insertions, 0 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index b44a3b2..0965536 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -1697,6 +1697,27 @@ class LoggerAdapter(object): """ return self.logger.hasHandlers() + def _log(self, level, msg, args, exc_info=None, extra=None, stack_info=False): + """ + Low-level log implementation, proxied to allow nested logger adapters. + """ + return self.logger._log( + level, + msg, + args, + exc_info=exc_info, + extra=extra, + stack_info=stack_info, + ) + + @property + def manager(self): + return self.logger.manager + + @manager.setter + def set_manager(self, value): + self.logger.manager = value + def __repr__(self): logger = self.logger level = getLevelName(logger.getEffectiveLevel()) diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 4ec02e1..25046c3 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -3955,6 +3955,17 @@ class LoggerAdapterTest(unittest.TestCase): self.assertFalse(self.logger.hasHandlers()) self.assertFalse(self.adapter.hasHandlers()) + def test_nested(self): + msg = 'Adapters can be nested, yo.' + adapter_adapter = logging.LoggerAdapter(logger=self.adapter, extra=None) + 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.args, (self.recording,)) + class LoggerTest(BaseTest): diff --git a/Misc/NEWS.d/next/Library/2017-09-13-13-33-39.bpo-31457.bIVBtI.rst b/Misc/NEWS.d/next/Library/2017-09-13-13-33-39.bpo-31457.bIVBtI.rst new file mode 100644 index 0000000..153aa42 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2017-09-13-13-33-39.bpo-31457.bIVBtI.rst @@ -0,0 +1 @@ +LoggerAdapter objects can now be nested. |