From 5ee9fb39bf41ab8f7c074becc15dcf3a3cb15449 Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Thu, 10 Jul 2014 09:42:35 +0300 Subject: [PATCH] log: don't automatically setup gbp-specific logger This makes it nicer to use gbp.* modules (e.g. gbp.git) in other software projects which may have their own logging setup. Now, importing gbp.* modules won't force-setup logging. Change-Id: I3e9cfe27c67cdd74c32ceba5dad786cc694ad7b6 Signed-off-by: Markus Lehtonen --- gbp/log.py | 23 +++++++++++++++++------ tests/component/__init__.py | 2 ++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/gbp/log.py b/gbp/log.py index 1e177d4..e269d65 100644 --- a/gbp/log.py +++ b/gbp/log.py @@ -20,9 +20,11 @@ import os import sys import logging -from logging import (DEBUG, INFO, WARNING, ERROR, CRITICAL, getLogger) +from logging import DEBUG, INFO, WARNING, ERROR, CRITICAL import gbp.tristate +# Initialize default logger +LOGGER = logging.getLogger(__name__) COLORS = dict([('none', 0)] + zip(['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'], range(30, 38))) @@ -162,8 +164,18 @@ def _parse_color_scheme(color_scheme=""): except KeyError: pass return scheme +def getLogger(*args, **kwargs): + """Gbp-specific function""" + if not issubclass(logging.getLoggerClass(), GbpLogger): + logging.setLoggerClass(GbpLogger) + return logging.getLogger(*args, **kwargs) + def setup(color, verbose, color_scheme=""): """Basic logger setup""" + # Initialize, if not done yet + if not isinstance(LOGGER, GbpLogger): + initialize() + LOGGER.set_color(color) LOGGER.set_color_scheme(_parse_color_scheme(color_scheme)) if verbose: @@ -171,9 +183,8 @@ def setup(color, verbose, color_scheme=""): else: LOGGER.setLevel(INFO) - -# Initialize the module -logging.setLoggerClass(GbpLogger) - -LOGGER = getLogger("gbp") +def initialize(): + """Initialize the logger module""" + global LOGGER + LOGGER = getLogger("gbp") diff --git a/tests/component/__init__.py b/tests/component/__init__.py index d8ad3e2..57d8692 100644 --- a/tests/component/__init__.py +++ b/tests/component/__init__.py @@ -91,6 +91,8 @@ class ComponentTestBase(object): # Prevent local config files from messing up the tests os.environ['GBP_CONF_FILES'] = '%(top_dir)s/.gbp.conf:' \ '%(top_dir)s/debian/gbp.conf:%(git_dir)s/gbp.conf' + # Initialize gbp logging module + gbp.log.initialize() @classmethod def teardown_class(cls): -- 2.7.4