2022-08-06 02:11:44 +02:00
|
|
|
import logging
|
2022-08-08 09:27:52 +02:00
|
|
|
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)
|
|
|
|
|
2022-08-06 02:11:44 +02:00
|
|
|
|
|
|
|
console_logger = logging.StreamHandler()
|
2022-08-08 09:27:52 +02:00
|
|
|
|
|
|
|
# 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")
|
|
|
|
|
2022-08-06 02:11:44 +02:00
|
|
|
# setup logger
|
|
|
|
console_logger.setFormatter(console_formatter)
|
|
|
|
log = logging.getLogger("rc")
|
|
|
|
log.addHandler(console_logger)
|
|
|
|
log.setLevel(logging.INFO)
|