Color records that are logged via logger.handle() as well.
This commit is contained in:
parent
a7ba97803f
commit
37362150fe
1 changed files with 33 additions and 14 deletions
|
@ -36,26 +36,42 @@ def should_do_markup(no_colour):
|
||||||
return sys.stdout.isatty() and os.environ.get('TERM') != 'dumb'
|
return sys.stdout.isatty() and os.environ.get('TERM') != 'dumb'
|
||||||
|
|
||||||
|
|
||||||
|
LOG_LEVEL_TO_COLOR = {
|
||||||
|
logging.CRITICAL: colorama.Fore.RED,
|
||||||
|
logging.WARN: colorama.Fore.YELLOW,
|
||||||
|
logging.INFO: colorama.Fore.GREEN,
|
||||||
|
logging.DEBUG: colorama.Fore.CYAN,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class BorgmaticLogger(logging.Logger):
|
class BorgmaticLogger(logging.Logger):
|
||||||
|
def critical(self, msg, *args, **kwargs):
|
||||||
|
color = LOG_LEVEL_TO_COLOR.get(logging.CRITICAL)
|
||||||
|
|
||||||
|
return super(BorgmaticLogger, self).critical(color_text(color, msg), *args, **kwargs)
|
||||||
|
|
||||||
def warn(self, msg, *args, **kwargs):
|
def warn(self, msg, *args, **kwargs):
|
||||||
return super(BorgmaticLogger, self).warn(
|
color = LOG_LEVEL_TO_COLOR.get(logging.WARN)
|
||||||
color_text(colorama.Fore.YELLOW, msg), *args, **kwargs
|
|
||||||
)
|
return super(BorgmaticLogger, self).warn(color_text(color, msg), *args, **kwargs)
|
||||||
|
|
||||||
def info(self, msg, *args, **kwargs):
|
def info(self, msg, *args, **kwargs):
|
||||||
return super(BorgmaticLogger, self).info(
|
color = LOG_LEVEL_TO_COLOR.get(logging.INFO)
|
||||||
color_text(colorama.Fore.GREEN, msg), *args, **kwargs
|
|
||||||
)
|
return super(BorgmaticLogger, self).info(color_text(color, msg), *args, **kwargs)
|
||||||
|
|
||||||
def debug(self, msg, *args, **kwargs):
|
def debug(self, msg, *args, **kwargs):
|
||||||
return super(BorgmaticLogger, self).debug(
|
color = LOG_LEVEL_TO_COLOR.get(logging.DEBUG)
|
||||||
color_text(colorama.Fore.CYAN, msg), *args, **kwargs
|
|
||||||
|
return super(BorgmaticLogger, self).debug(color_text(color, msg), *args, **kwargs)
|
||||||
|
|
||||||
|
def handle(self, record):
|
||||||
|
color = LOG_LEVEL_TO_COLOR.get(record.levelno)
|
||||||
|
colored_record = logging.makeLogRecord(
|
||||||
|
dict(levelno=record.levelno, msg=color_text(color, record.msg))
|
||||||
)
|
)
|
||||||
|
|
||||||
def critical(self, msg, *args, **kwargs):
|
return super(BorgmaticLogger, self).handle(colored_record)
|
||||||
return super(BorgmaticLogger, self).critical(
|
|
||||||
color_text(colorama.Fore.RED, msg), *args, **kwargs
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def get_logger(name=None):
|
def get_logger(name=None):
|
||||||
|
@ -68,8 +84,11 @@ def get_logger(name=None):
|
||||||
return logger
|
return logger
|
||||||
|
|
||||||
|
|
||||||
def color_text(color, msg):
|
def color_text(color, message):
|
||||||
'''
|
'''
|
||||||
Give colored text.
|
Give colored text.
|
||||||
'''
|
'''
|
||||||
return '{}{}{}'.format(color, msg, colorama.Style.RESET_ALL)
|
if not color:
|
||||||
|
return message
|
||||||
|
|
||||||
|
return '{}{}{}'.format(color, message, colorama.Style.RESET_ALL)
|
||||||
|
|
Loading…
Reference in a new issue