diff options
author | Avram Lubkin <git_hub@avram.us> | 2017-07-30 09:36:33 (GMT) |
---|---|---|
committer | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2017-07-30 09:36:33 (GMT) |
commit | 78c18a9b9a1445f7c755929917a790ba02b4a5e0 (patch) | |
tree | 4632a83a3fd2ac4d9902b3556b4cb5381d85426d /Lib/logging | |
parent | caa1280d1ee5f828f346b585169a7592371d3faa (diff) | |
download | cpython-78c18a9b9a1445f7c755929917a790ba02b4a5e0.zip cpython-78c18a9b9a1445f7c755929917a790ba02b4a5e0.tar.gz cpython-78c18a9b9a1445f7c755929917a790ba02b4a5e0.tar.bz2 |
bpo-30962: Added caching to Logger.isEnabledFor() (GH-2752)
Diffstat (limited to 'Lib/logging')
-rw-r--r-- | Lib/logging/__init__.py | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 64e24ee..f9bfb79 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -1244,6 +1244,19 @@ class Manager(object): alogger.parent = c.parent c.parent = alogger + def _clear_cache(self): + """ + Clear the cache for all loggers in loggerDict + Called when level changes are made + """ + + _acquireLock() + for logger in self.loggerDict.values(): + if isinstance(logger, Logger): + logger._cache.clear() + self.root._cache.clear() + _releaseLock() + #--------------------------------------------------------------------------- # Logger classes and functions #--------------------------------------------------------------------------- @@ -1274,12 +1287,14 @@ class Logger(Filterer): self.propagate = True self.handlers = [] self.disabled = False + self._cache = {} def setLevel(self, level): """ Set the logging level of this logger. level must be an int or a str. """ self.level = _checkLevel(level) + self.manager._clear_cache() def debug(self, msg, *args, **kwargs): """ @@ -1543,9 +1558,17 @@ class Logger(Filterer): """ Is this logger enabled for level 'level'? """ - if self.manager.disable >= level: - return False - return level >= self.getEffectiveLevel() + try: + return self._cache[level] + except KeyError: + _acquireLock() + if self.manager.disable >= level: + is_enabled = self._cache[level] = False + else: + is_enabled = self._cache[level] = level >= self.getEffectiveLevel() + _releaseLock() + + return is_enabled def getChild(self, suffix): """ @@ -1910,6 +1933,7 @@ def disable(level=CRITICAL): Disable all logging calls of severity 'level' and below. """ root.manager.disable = level + root.manager._clear_cache() def shutdown(handlerList=_handlerList): """ |