comparison sat/core/log.py @ 2836:ad00f61fd9f5

core (log): add traceback when "exc_info" is set
author Goffi <goffi@goffi.org>
date Sun, 03 Mar 2019 12:04:55 +0100
parents 003b8b4b56a7
children ab2696e34d29
comparison
equal deleted inserted replaced
2835:6aa22011bc6d 2836:ad00f61fd9f5
22 # TODO: change formatting from "%s" style to "{}" when moved to Python 3 22 # TODO: change formatting from "%s" style to "{}" when moved to Python 3
23 23
24 from sat.core.constants import Const as C 24 from sat.core.constants import Const as C
25 from sat.tools.common.ansi import ANSI as A 25 from sat.tools.common.ansi import ANSI as A
26 from sat.core import exceptions 26 from sat.core import exceptions
27 import traceback
27 28
28 backend = None 29 backend = None
29 _loggers = {} 30 _loggers = {}
30 handlers = {} 31 handlers = {}
31 COLOR_START = '%(color_start)s' 32 COLOR_START = '%(color_start)s'
53 self.fmt = other.fmt 54 self.fmt = other.fmt
54 self.Filter_name = other.fmt 55 self.Filter_name = other.fmt
55 self.post_treat = other.post_treat 56 self.post_treat = other.post_treat
56 self._name = other._name 57 self._name = other._name
57 58
58 def out(self, message, level=None): 59 def addTraceback(self, message):
60 tb = traceback.format_exc()
61 return message + "\n==== traceback ====\n" + tb
62
63 def out(self, message, level=None, **kwargs):
59 """Actually log the message 64 """Actually log the message
60 65
61 @param message: formatted message 66 @param message: formatted message
62 """ 67 """
68 if kwargs.get('exc_info', False):
69 message = self.addTraceback(message)
63 print message 70 print message
64 71
65 def log(self, level, message): 72 def log(self, level, message, **kwargs):
66 """Print message 73 """Print message
67 74
68 @param level: one of C.LOG_LEVELS 75 @param level: one of C.LOG_LEVELS
69 @param message: message to format and print 76 @param message: message to format and print
70 """ 77 """
71 try: 78 try:
72 formatted = self.format(level, message) 79 formatted = self.format(level, message)
73 if self.post_treat is None: 80 if self.post_treat is None:
74 self.out(formatted, level) 81 self.out(formatted, level, **kwargs)
75 else: 82 else:
76 self.out(self.post_treat(level, formatted), level) 83 self.out(self.post_treat(level, formatted), level, **kwargs)
77 except Filtered: 84 except Filtered:
78 pass 85 pass
79 86
80 def format(self, level, message): 87 def format(self, level, message):
81 """Format message according to Logger.fmt 88 """Format message according to Logger.fmt
108 record['profile'] = configure_cls[backend].getProfile() 115 record['profile'] = configure_cls[backend].getProfile()
109 return self.fmt % record 116 return self.fmt % record
110 else: 117 else:
111 raise e 118 raise e
112 119
113 def debug(self, msg): 120 def debug(self, msg, **kwargs):
114 self.log(C.LOG_LVL_DEBUG, msg) 121 self.log(C.LOG_LVL_DEBUG, msg, **kwargs)
115 122
116 def info(self, msg): 123 def info(self, msg, **kwargs):
117 self.log(C.LOG_LVL_INFO, msg) 124 self.log(C.LOG_LVL_INFO, msg, **kwargs)
118 125
119 def warning(self, msg): 126 def warning(self, msg, **kwargs):
120 self.log(C.LOG_LVL_WARNING, msg) 127 self.log(C.LOG_LVL_WARNING, msg, **kwargs)
121 128
122 def error(self, msg): 129 def error(self, msg, **kwargs):
123 self.log(C.LOG_LVL_ERROR, msg) 130 self.log(C.LOG_LVL_ERROR, msg, **kwargs)
124 131
125 def critical(self, msg): 132 def critical(self, msg, **kwargs):
126 self.log(C.LOG_LVL_CRITICAL, msg) 133 self.log(C.LOG_LVL_CRITICAL, msg, **kwargs)
127 134
128 135
129 class FilterName(object): 136 class FilterName(object):
130 """Filter on logger name according to a regex""" 137 """Filter on logger name according to a regex"""
131 138
377 raise ValueError("This method should not be called with backend [{}]".format(backend)) 384 raise ValueError("This method should not be called with backend [{}]".format(backend))
378 return _loggers.setdefault(name, logger_class(name)) 385 return _loggers.setdefault(name, logger_class(name))
379 386
380 _root_logger = getLogger() 387 _root_logger = getLogger()
381 388
382 def debug(msg): 389 def debug(msg, **kwargs):
383 _root_logger.debug(msg) 390 _root_logger.debug(msg, **kwargs)
384 391
385 def info(msg): 392 def info(msg, **kwargs):
386 _root_logger.info(msg) 393 _root_logger.info(msg, **kwargs)
387 394
388 def warning(msg): 395 def warning(msg, **kwargs):
389 _root_logger.warning(msg) 396 _root_logger.warning(msg, **kwargs)
390 397
391 def error(msg): 398 def error(msg, **kwargs):
392 _root_logger.error(msg) 399 _root_logger.error(msg, **kwargs)
393 400
394 def critical(msg): 401 def critical(msg, **kwargs):
395 _root_logger.critical(msg) 402 _root_logger.critical(msg, **kwargs)