repo-cloner/repo_cloner/lib/logger_setup.py

46 lines
1.3 KiB
Python
Raw Normal View History

import logging
import sys
class CustomFormatter(logging.Formatter):
grey = "\x1b[38;20m"
light_blue = "\x1b[34;1m"
yellow = "\x1b[33;20m"
red = "\x1b[31;20m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
# format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)"
format = "%(asctime)-15s.%(msecs)03d :: [%(levelname)8s] :: %(name)-15s :: %(message)s (%(filename)s:%(lineno)s)"
time_format = "%Y-%m-%d %H:%M:%S"
FORMATS = {
logging.DEBUG: grey + format + reset,
logging.INFO: light_blue + format + reset,
logging.WARNING: yellow + format + reset,
logging.ERROR: red + format + reset,
logging.CRITICAL: bold_red + format + reset
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt, self.time_format)
return formatter.format(record)
console_logger = logging.StreamHandler()
# formatter for stdout - colored
if sys.stdout.isatty() or "--colored" in sys.argv:
console_formatter = CustomFormatter()
else:
# b&w for rest
console_formatter = logging.Formatter(
"%(asctime)-15s.%(msecs)03d :: [%(levelname)8s] :: %(name)-15s :: %(message)s (%(filename)s:%(lineno)s)",
"%Y-%m-%d %H:%M:%S")
# setup logger
console_logger.setFormatter(console_formatter)
log = logging.getLogger("rc")
log.addHandler(console_logger)
log.setLevel(logging.INFO)