[Python-checkins] bpo-31457: Allow for nested LoggerAdapter objects (#3551)
Łukasz Langa
webhook-mailer at python.org
Thu Sep 14 11:34:49 EDT 2017
https://github.com/python/cpython/commit/1bbd482bcf6ea36bfe488f868810ffe110238ae1
commit: 1bbd482bcf6ea36bfe488f868810ffe110238ae1
branch: master
author: Łukasz Langa <lukasz at langa.pl>
committer: GitHub <noreply at github.com>
date: 2017-09-14T11:34:47-04:00
summary:
bpo-31457: Allow for nested LoggerAdapter objects (#3551)
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.
files:
A Misc/NEWS.d/next/Library/2017-09-13-13-33-39.bpo-31457.bIVBtI.rst
M Lib/logging/__init__.py
M Lib/test/test_logging.py
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index 19b96b813cf..b941eab97d6 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -1739,6 +1739,27 @@ def hasHandlers(self):
"""
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 76f98bb572d..611044d8fa8 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -3986,6 +3986,17 @@ def test_has_handlers(self):
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 00000000000..153aa42a62e
--- /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.
More information about the Python-checkins
mailing list