From e021ecaa4ff2aeaff4d103d391811fb6c88f68c7 Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Thu, 11 Sep 2014 15:25:00 +0300 Subject: [PATCH] log: don't initialize handlers in GbpLogger init Better compatibility with 3rd party modules that have their own logging initializations. Change-Id: Ic91ab54c70537061c3e76dd6317ceeec888fab36 Signed-off-by: Markus Lehtonen --- gbp/log.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/gbp/log.py b/gbp/log.py index a8e420a..a508b67 100644 --- a/gbp/log.py +++ b/gbp/log.py @@ -96,31 +96,35 @@ class GbpStreamHandler(logging.StreamHandler): class GbpLogger(logging.Logger): """Logger class for git-buildpackage""" - def __init__(self, name, color='auto', *args, **kwargs): + def __init__(self, name, *args, **kwargs): logging.Logger.__init__(self, name, *args, **kwargs) - self._default_handlers = [GbpStreamHandler(sys.stdout, color), - GbpStreamHandler(sys.stderr, color)] - self._default_handlers[0].addFilter(GbpFilter([DEBUG, INFO])) - self._default_handlers[1].addFilter(GbpFilter([WARNING, ERROR, - CRITICAL])) - for hdlr in self._default_handlers: + self.default_handlers = [] + + def init_default_handlers(self, color='auto'): + """Initialize and set default handlers to logger""" + self.default_handlers = [GbpStreamHandler(sys.stdout, color), + GbpStreamHandler(sys.stderr, color)] + self.default_handlers[0].addFilter(GbpFilter([DEBUG, INFO])) + self.default_handlers[1].addFilter(GbpFilter([WARNING, ERROR, + CRITICAL])) + for hdlr in self.default_handlers: self.addHandler(hdlr) # We don't want to propagate as we add our own handlers self.propagate = False def set_color(self, color): """Set/unset colorized output of the default handlers""" - for hdlr in self._default_handlers: + for hdlr in self.default_handlers: hdlr.set_color(color) def set_color_scheme(self, color_scheme={}): """Set the color scheme of the default handlers""" - for hdlr in self._default_handlers: + for hdlr in self.default_handlers: hdlr.set_color_scheme(color_scheme) def set_format(self, fmt): """Set the format of the default handlers""" - for hdlr in self._default_handlers: + for hdlr in self.default_handlers: hdlr.set_format(fmt) @@ -170,7 +174,11 @@ def getLogger(*args, **kwargs): """Gbp-specific function""" if not issubclass(logging.getLoggerClass(), GbpLogger): logging.setLoggerClass(GbpLogger) - return logging.getLogger(*args, **kwargs) + color = kwargs.pop('color') if 'color' in kwargs else 'auto' + logger = logging.getLogger(*args, **kwargs) + if not logger.default_handlers: + logger.init_default_handlers(color) + return logger def setup(color, verbose, color_scheme=""): """Basic logger setup""" -- 2.7.4