[Python-checkins] bpo-33606: improve logging performance when logger is disabled (GH-7285)

Vinay Sajip webhook-mailer at python.org
Fri Jun 1 03:29:49 EDT 2018


https://github.com/python/cpython/commit/6e3ca645e71dd021fead5a70dc06d9b663612e3a
commit: 6e3ca645e71dd021fead5a70dc06d9b663612e3a
branch: master
author: Timo Furrer <tuxtimo at gmail.com>
committer: Vinay Sajip <vinay_sajip at yahoo.co.uk>
date: 2018-06-01T08:29:46+01:00
summary:

bpo-33606: improve logging performance when logger is disabled (GH-7285)

A check has been added in Logger.isEnabledFor() to return False when the logger is disabled. This avoids unnecessary work being done when a disabled logger is used.

files:
M Lib/logging/__init__.py
M Lib/test/test_logging.py

diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index 46c590687c7d..a3617b16c4d1 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -1569,6 +1569,9 @@ def isEnabledFor(self, level):
         """
         Is this logger enabled for level 'level'?
         """
+        if self.disabled:
+            return False
+
         try:
             return self._cache[level]
         except KeyError:
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
index eee2ed0c19ec..e02bb31c3392 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -4097,6 +4097,18 @@ def test_is_enabled_for(self):
         self.addCleanup(setattr, self.logger.manager, 'disable', old_disable)
         self.assertFalse(self.logger.isEnabledFor(22))
 
+    def test_is_enabled_for_disabled_logger(self):
+        old_disabled = self.logger.disabled
+        old_disable = self.logger.manager.disable
+
+        self.logger.disabled = True
+        self.logger.manager.disable = 21
+
+        self.addCleanup(setattr, self.logger, 'disabled', old_disabled)
+        self.addCleanup(setattr, self.logger.manager, 'disable', old_disable)
+
+        self.assertFalse(self.logger.isEnabledFor(22))
+
     def test_root_logger_aliases(self):
         root = logging.getLogger()
         self.assertIs(root, logging.root)



More information about the Python-checkins mailing list