From 12a9d7f950a949c2b0d7968e9a11225b83eccac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Doktor?= Date: Fri, 12 May 2017 17:45:34 +0200 Subject: [PATCH] utils.stacktrace: Allow passing logger as well as log name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sometimes we already have the logger so it makes a little sense to pass logger name only to obtain the logger again in this function. Lets allow both. Signed-off-by: Lukáš Doktor --- avocado/utils/stacktrace.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/avocado/utils/stacktrace.py b/avocado/utils/stacktrace.py index 3c9f8a07..6221a95a 100644 --- a/avocado/utils/stacktrace.py +++ b/avocado/utils/stacktrace.py @@ -32,17 +32,18 @@ def log_exc_info(exc_info, logger=''): Log exception info to logger_name. :param exc_info: Exception info produced by sys.exc_info() - :param logger: Name of the logger (defaults to '') + :param logger: Name or logger instance (defaults to '') """ - log = logging.getLogger(logger) - log.error('') + if isinstance(logger, basestring): + logger = logging.getLogger(logger) + logger.error('') called_from = inspect.currentframe().f_back - log.error("Reproduced traceback from: %s:%s", - called_from.f_code.co_filename, called_from.f_lineno) + logger.error("Reproduced traceback from: %s:%s", + called_from.f_code.co_filename, called_from.f_lineno) for line in tb_info(exc_info): for l in line.splitlines(): - log.error(l) - log.error('') + logger.error(l) + logger.error('') def log_message(message, logger=''): @@ -50,11 +51,12 @@ def log_message(message, logger=''): Log message to logger. :param message: Message - :param logger: Name of the logger (defaults to '') + :param logger: Name or logger instance (defaults to '') """ - log = logging.getLogger(logger) + if isinstance(logger, basestring): + logger = logging.getLogger(logger) for line in message.splitlines(): - log.error(line) + logger.error(line) def analyze_unpickable_item(path_prefix, obj): -- GitLab