borgmatic/borgmatic/logger.py

124 lines
3.3 KiB
Python
Raw Normal View History

2019-05-12 09:30:22 +00:00
import logging
import os
import sys
import colorama
def to_bool(arg):
'''
Return a boolean value based on `arg`.
'''
if arg is None or isinstance(arg, bool):
return arg
if isinstance(arg, str):
arg = arg.lower()
if arg in ('yes', 'on', '1', 'true', 1):
return True
return False
def should_do_markup(no_color):
2019-05-12 09:30:22 +00:00
'''
Determine if we should enable colorama marking up.
'''
if no_color:
2019-05-12 09:30:22 +00:00
return False
py_colors = os.environ.get('PY_COLORS', None)
if py_colors is not None:
return to_bool(py_colors)
return sys.stdout.isatty() and os.environ.get('TERM') != 'dumb'
LOG_LEVEL_TO_COLOR = {
logging.CRITICAL: colorama.Fore.RED,
2019-05-14 04:10:26 +00:00
logging.ERROR: colorama.Fore.RED,
logging.WARN: colorama.Fore.YELLOW,
logging.INFO: colorama.Fore.GREEN,
logging.DEBUG: colorama.Fore.CYAN,
}
2019-05-14 04:10:26 +00:00
class Borgmatic_logger(logging.Logger):
def critical(self, msg, *args, **kwargs):
color = LOG_LEVEL_TO_COLOR.get(logging.CRITICAL)
2019-05-14 04:10:26 +00:00
return super(Borgmatic_logger, self).critical(color_text(color, msg), *args, **kwargs)
def error(self, msg, *args, **kwargs):
color = LOG_LEVEL_TO_COLOR.get(logging.ERROR)
return super(Borgmatic_logger, self).error(color_text(color, msg), *args, **kwargs)
2019-05-12 09:30:22 +00:00
def warn(self, msg, *args, **kwargs):
color = LOG_LEVEL_TO_COLOR.get(logging.WARN)
2019-05-14 04:10:26 +00:00
return super(Borgmatic_logger, self).warn(color_text(color, msg), *args, **kwargs)
2019-05-12 09:30:22 +00:00
def info(self, msg, *args, **kwargs):
color = LOG_LEVEL_TO_COLOR.get(logging.INFO)
2019-05-14 04:10:26 +00:00
return super(Borgmatic_logger, self).info(color_text(color, msg), *args, **kwargs)
2019-05-12 09:30:22 +00:00
def debug(self, msg, *args, **kwargs):
color = LOG_LEVEL_TO_COLOR.get(logging.DEBUG)
2019-05-12 09:30:22 +00:00
2019-05-14 04:10:26 +00:00
return super(Borgmatic_logger, 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,
levelname=record.levelname,
msg=color_text(color, record.msg),
)
2019-05-12 09:30:22 +00:00
)
2019-05-14 04:10:26 +00:00
return super(Borgmatic_logger, self).handle(colored_record)
2019-05-12 09:30:22 +00:00
def get_logger(name=None):
'''
Build a logger with the given name.
'''
2019-05-14 04:10:26 +00:00
logging.setLoggerClass(Borgmatic_logger)
2019-05-12 09:30:22 +00:00
logger = logging.getLogger(name)
return logger
def color_text(color, message):
2019-05-12 09:30:22 +00:00
'''
Give colored text.
'''
if not color:
return message
return '{}{}{}'.format(color, message, colorama.Style.RESET_ALL)
2019-05-27 22:44:48 +00:00
def configure_logging(console_log_level, syslog_log_level=None):
'''
2019-05-27 22:44:48 +00:00
Configure logging to go to both the console and syslog. Use the given log levels, respectively.
'''
2019-05-27 22:44:48 +00:00
if syslog_log_level is None:
syslog_log_level = console_log_level
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter('%(message)s'))
console_handler.setLevel(console_log_level)
syslog_handler = logging.handlers.SysLogHandler(address='/dev/log')
syslog_handler.setFormatter(logging.Formatter('borgmatic: %(levelname)s %(message)s'))
2019-05-27 22:44:48 +00:00
syslog_handler.setLevel(syslog_log_level)
logging.basicConfig(
2019-05-27 22:46:38 +00:00
level=min(console_log_level, syslog_log_level), handlers=(console_handler, syslog_handler)
)