From 3b84eae03ebd8122fdbdced3d85999dd9aedfc7e Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Sat, 25 May 2013 03:20:34 -0700 Subject: Closes #18046: Simplified logging internals relating to levels and their names. Thanks to Alex Gaynor for the patch. --- Lib/logging/__init__.py | 40 +++++++++++++++++++++------------------- Lib/logging/config.py | 6 +++--- Lib/test/test_logging.py | 9 ++++++--- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 244c915..4629860 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -123,20 +123,22 @@ INFO = 20 DEBUG = 10 NOTSET = 0 -_levelNames = { - CRITICAL : 'CRITICAL', - ERROR : 'ERROR', - WARNING : 'WARNING', - INFO : 'INFO', - DEBUG : 'DEBUG', - NOTSET : 'NOTSET', - 'CRITICAL' : CRITICAL, - 'ERROR' : ERROR, - 'WARN' : WARNING, - 'WARNING' : WARNING, - 'INFO' : INFO, - 'DEBUG' : DEBUG, - 'NOTSET' : NOTSET, +_levelToName = { + CRITICAL: 'CRITICAL', + ERROR: 'ERROR', + WARNING: 'WARNING', + INFO: 'INFO', + DEBUG: 'DEBUG', + NOTSET: 'NOTSET', +} +_nameToLevel = { + 'CRITICAL': CRITICAL, + 'ERROR': ERROR, + 'WARN': WARNING, + 'WARNING': WARNING, + 'INFO': INFO, + 'DEBUG': DEBUG, + 'NOTSET': NOTSET, } def getLevelName(level): @@ -153,7 +155,7 @@ def getLevelName(level): Otherwise, the string "Level %s" % level is returned. """ - return _levelNames.get(level, ("Level %s" % level)) + return _levelToName.get(level, ("Level %s" % level)) def addLevelName(level, levelName): """ @@ -163,8 +165,8 @@ def addLevelName(level, levelName): """ _acquireLock() try: #unlikely to cause an exception, but you never know... - _levelNames[level] = levelName - _levelNames[levelName] = level + _levelToName[level] = levelName + _nameToLevel[levelName] = level finally: _releaseLock() @@ -172,9 +174,9 @@ def _checkLevel(level): if isinstance(level, int): rv = level elif str(level) == level: - if level not in _levelNames: + if level not in _nameToLevel: raise ValueError("Unknown level: %r" % level) - rv = _levelNames[level] + rv = _nameToLevel[level] else: raise TypeError("Level not an integer or a valid string: %r" % level) return rv diff --git a/Lib/logging/config.py b/Lib/logging/config.py index dbfd2c6..b882a62 100644 --- a/Lib/logging/config.py +++ b/Lib/logging/config.py @@ -144,7 +144,7 @@ def _install_handlers(cp, formatters): h = klass(*args) if "level" in section: level = section["level"] - h.setLevel(logging._levelNames[level]) + h.setLevel(level) if len(fmt): h.setFormatter(formatters[fmt]) if issubclass(klass, logging.handlers.MemoryHandler): @@ -191,7 +191,7 @@ def _install_loggers(cp, handlers, disable_existing): log = root if "level" in section: level = section["level"] - log.setLevel(logging._levelNames[level]) + log.setLevel(level) for h in root.handlers[:]: root.removeHandler(h) hlist = section["handlers"] @@ -237,7 +237,7 @@ def _install_loggers(cp, handlers, disable_existing): existing.remove(qn) if "level" in section: level = section["level"] - logger.setLevel(logging._levelNames[level]) + logger.setLevel(level) for h in logger.handlers[:]: logger.removeHandler(h) logger.propagate = propagate diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index edb1c63..aa878da 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -94,7 +94,8 @@ class BaseTest(unittest.TestCase): self.saved_handlers = logging._handlers.copy() self.saved_handler_list = logging._handlerList[:] self.saved_loggers = saved_loggers = logger_dict.copy() - self.saved_level_names = logging._levelNames.copy() + self.saved_name_to_level = logging._nameToLevel.copy() + self.saved_level_to_name = logging._levelToName.copy() self.logger_states = logger_states = {} for name in saved_loggers: logger_states[name] = getattr(saved_loggers[name], @@ -136,8 +137,10 @@ class BaseTest(unittest.TestCase): self.root_logger.setLevel(self.original_logging_level) logging._acquireLock() try: - logging._levelNames.clear() - logging._levelNames.update(self.saved_level_names) + logging._levelToName.clear() + logging._levelToName.update(self.saved_level_to_name) + logging._nameToLevel.clear() + logging._nameToLevel.update(self.saved_name_to_level) logging._handlers.clear() logging._handlers.update(self.saved_handlers) logging._handlerList[:] = self.saved_handler_list -- cgit v0.12