From 18836a44952929a47f5f65d93667472816e9eb8f Mon Sep 17 00:00:00 2001 From: Marco Capitani Date: Fri, 21 Apr 2017 16:56:03 +0200 Subject: rumba: log: Simplified configuration, small fixes --- rumba/log.py | 53 +++++++++++++++++++++-------------------------------- 1 file changed, 21 insertions(+), 32 deletions(-) (limited to 'rumba/log.py') diff --git a/rumba/log.py b/rumba/log.py index c9c3c01..ebb76dc 100644 --- a/rumba/log.py +++ b/rumba/log.py @@ -5,46 +5,35 @@ import sys import multiprocessing -config_lock = multiprocessing.Lock() +def setup(): + handler = logging.StreamHandler(sys.stdout) + handler.lock = multiprocessing.RLock() + logging.basicConfig(format='{asctime} | {levelname:8.8} | ' + '{name:15.15} | {message}', + style='{', + handlers=[handler], + level=logging.DEBUG) + logging.getLogger('').setLevel(logging.ERROR) -class _LoggingConfigurator(object): +# Used for the first call, in order to configure logging +def _get_logger_with_setup(name): + setup() + # Swap _get_logger implementation to the setup-less version. + global _get_logger + _get_logger = _get_logger_without_setup + return logging.getLogger(name) - is_done = False - def setup(self): - with config_lock: - if not self.is_done: - # Double check, so we only configure once. - handler = logging.StreamHandler(sys.stdout) - handler.lock = multiprocessing.RLock() - logging.basicConfig(format='{asctime} | {levelname:8.8} | ' - '{name:15.15} | {message}', - style='{', - handlers=[handler], - level=logging.DEBUG) - logging.getLogger('').setLevel(logging.ERROR) - global configurator - configurator = _SkipLoggingConfigurator() - else: - pass +# Then this one is used. +def _get_logger_without_setup(name): + return logging.getLogger(name) - def get_logger(self, name): - self.setup() - return logging.getLogger(name) - -class _SkipLoggingConfigurator(object): - - @staticmethod - def get_logger(name): - return logging.getLogger(name) - - -configurator = _LoggingConfigurator() +_get_logger = _get_logger_with_setup def get_logger(name): - logger = configurator.get_logger(name.split('.')[-1]) + logger = _get_logger(name.split('.')[-1]) logger.setLevel(logging.DEBUG) return logger -- cgit v1.2.3