Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Lib/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,8 @@ def __init__(self, rootnode):
self.disable = 0
self.emittedNoHandlerWarning = False
self.loggerDict = {}
# Loggers whose isEnabledFor cache may be populated; see _clear_cache.
self._cached_loggers = weakref.WeakSet()
self.loggerClass = None
self.logRecordFactory = None

Expand Down Expand Up @@ -1471,9 +1473,12 @@ def _clear_cache(self):
"""

with _lock:
for logger in self.loggerDict.values():
if isinstance(logger, Logger):
# Only loggers that have populated a cache are visited, so
# setLevel() costs O(loggers with cached results), not O(loggers).
for logger in list(self._cached_loggers):
if self.loggerDict.get(logger.name) is logger:
logger._cache.clear()
self._cached_loggers.discard(logger)
self.root._cache.clear()

#---------------------------------------------------------------------------
Expand Down Expand Up @@ -1799,6 +1804,7 @@ def isEnabledFor(self, level):
return self._cache[level]
except KeyError:
with _lock:
self.manager._cached_loggers.add(self)
if self.manager.disable >= level:
is_enabled = self._cache[level] = False
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Speed up :meth:`logging.Logger.setLevel` and :func:`logging.disable` when there are many loggers: only loggers that have cached results are now cleared, instead of every logger.
Loading
⌂ Proxy