From 04f485057b8d4820874cedd1e4bdf149e2767875 Mon Sep 17 00:00:00 2001 From: Goffi Date: Sun, 3 Mar 2019 12:04:55 +0100 Subject: [PATCH] core (log): add traceback when "exc_info" is set --- sat/core/log.py | 55 ++++++++++++++++++++++++------------------ sat/core/log_config.py | 4 ++- sat/tools/config.py | 2 +- 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/sat/core/log.py b/sat/core/log.py index 6abb9f12..4ef93d51 100644 --- a/sat/core/log.py +++ b/sat/core/log.py @@ -24,6 +24,7 @@ from sat.core.constants import Const as C from sat.tools.common.ansi import ANSI as A from sat.core import exceptions +import traceback backend = None _loggers = {} @@ -55,14 +56,20 @@ class Logger(object): self.post_treat = other.post_treat self._name = other._name - def out(self, message, level=None): + def addTraceback(self, message): + tb = traceback.format_exc() + return message + "\n==== traceback ====\n" + tb + + def out(self, message, level=None, **kwargs): """Actually log the message @param message: formatted message """ + if kwargs.get('exc_info', False): + message = self.addTraceback(message) print message - def log(self, level, message): + def log(self, level, message, **kwargs): """Print message @param level: one of C.LOG_LEVELS @@ -71,9 +78,9 @@ class Logger(object): try: formatted = self.format(level, message) if self.post_treat is None: - self.out(formatted, level) + self.out(formatted, level, **kwargs) else: - self.out(self.post_treat(level, formatted), level) + self.out(self.post_treat(level, formatted), level, **kwargs) except Filtered: pass @@ -110,20 +117,20 @@ class Logger(object): else: raise e - def debug(self, msg): - self.log(C.LOG_LVL_DEBUG, msg) + def debug(self, msg, **kwargs): + self.log(C.LOG_LVL_DEBUG, msg, **kwargs) - def info(self, msg): - self.log(C.LOG_LVL_INFO, msg) + def info(self, msg, **kwargs): + self.log(C.LOG_LVL_INFO, msg, **kwargs) - def warning(self, msg): - self.log(C.LOG_LVL_WARNING, msg) + def warning(self, msg, **kwargs): + self.log(C.LOG_LVL_WARNING, msg, **kwargs) - def error(self, msg): - self.log(C.LOG_LVL_ERROR, msg) + def error(self, msg, **kwargs): + self.log(C.LOG_LVL_ERROR, msg, **kwargs) - def critical(self, msg): - self.log(C.LOG_LVL_CRITICAL, msg) + def critical(self, msg, **kwargs): + self.log(C.LOG_LVL_CRITICAL, msg, **kwargs) class FilterName(object): @@ -379,17 +386,17 @@ def getLogger(name=C.LOG_BASE_LOGGER): _root_logger = getLogger() -def debug(msg): - _root_logger.debug(msg) +def debug(msg, **kwargs): + _root_logger.debug(msg, **kwargs) -def info(msg): - _root_logger.info(msg) +def info(msg, **kwargs): + _root_logger.info(msg, **kwargs) -def warning(msg): - _root_logger.warning(msg) +def warning(msg, **kwargs): + _root_logger.warning(msg, **kwargs) -def error(msg): - _root_logger.error(msg) +def error(msg, **kwargs): + _root_logger.error(msg, **kwargs) -def critical(msg): - _root_logger.critical(msg) +def critical(msg, **kwargs): + _root_logger.critical(msg, **kwargs) diff --git a/sat/core/log_config.py b/sat/core/log_config.py index aa7ae4b9..a4a74097 100644 --- a/sat/core/log_config.py +++ b/sat/core/log_config.py @@ -34,11 +34,13 @@ class TwistedLogger(log.Logger): self.twisted_log = twisted_log - def out(self, message, level=None): + def out(self, message, level=None, **kwargs): """Actually log the message @param message: formatted message """ + if kwargs.get('exc_info', False): + message = self.addTraceback(message) self.twisted_log.msg( message.encode("utf-8", "ignore"), sat_logged=True, level=level ) diff --git a/sat/tools/config.py b/sat/tools/config.py index 9a8d394a..f0894b08 100644 --- a/sat/tools/config.py +++ b/sat/tools/config.py @@ -75,7 +75,7 @@ def parseMainConf(): try: config.read(C.CONFIG_FILES) except Exception as e: - log.error(_(u"Can't read main config: {msg}").format(msg=e)) + log.error(_(u"Can't read main config: {msg}").format(msg=e), exc_info=True) return config