From d245b75b66b5bc908998fe2e04df248f670834f7 Mon Sep 17 00:00:00 2001 From: LaraStuStu Date: Sat, 28 Mar 2020 14:02:40 +0800 Subject: [PATCH] Create logger.py --- DataAnnotation/labelme/labelme/logger.py | 51 ++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 DataAnnotation/labelme/labelme/logger.py diff --git a/DataAnnotation/labelme/labelme/logger.py b/DataAnnotation/labelme/labelme/logger.py new file mode 100644 index 0000000..f826771 --- /dev/null +++ b/DataAnnotation/labelme/labelme/logger.py @@ -0,0 +1,51 @@ +import logging + +import termcolor + +from . import __appname__ + + +COLORS = { + 'WARNING': 'yellow', + 'INFO': 'white', + 'DEBUG': 'blue', + 'CRITICAL': 'red', + 'ERROR': 'red', +} + + +class ColoredFormatter(logging.Formatter): + + def __init__(self, msg, use_color=True): + logging.Formatter.__init__(self, msg) + self.use_color = use_color + + def format(self, record): + levelname = record.levelname + if self.use_color and levelname in COLORS: + colored_levelname = termcolor.colored( + '[{}]'.format(levelname), color=COLORS[levelname] + ) + record.levelname = colored_levelname + return logging.Formatter.format(self, record) + + +class ColoredLogger(logging.Logger): + + fmt_filename = termcolor.colored('%(filename)s', attrs={'bold': True}) + FORMAT = '%(levelname)s %(message)s ({}:%(lineno)d)'.format(fmt_filename) + + def __init__(self, name): + logging.Logger.__init__(self, name, logging.INFO) + + color_formatter = ColoredFormatter(self.FORMAT) + + console = logging.StreamHandler() + console.setFormatter(color_formatter) + + self.addHandler(console) + return + + +logging.setLoggerClass(ColoredLogger) +logger = logging.getLogger(__appname__) -- GitLab