5 from contextlib
import contextmanager
9 logging.addLevelName(VERBOSE_LEVEL,
"VERBOSE")
12 logging.addLevelName(logging.CRITICAL,
"FATAL")
15 RED, YELLOW, BLUE, WHITE = 1, 3, 4, 7
19 COLOR_SEQ =
"\033[1;%dm"
23 return message.replace(
"$RESET", RESET_SEQ).
replace(
"$BOLD", BOLD_SEQ)
37 logging.Formatter.__init__(self, msg)
41 levelname = record.levelname
42 if self.
use_color and levelname
in COLORS:
43 color_seq = COLOR_SEQ % (30 + COLORS[levelname])
44 record.levelname = color_seq + levelname + RESET_SEQ
45 return logging.Formatter.format(self, record)
48 LoggerClass = logging.getLoggerClass()
49 @logging.setLoggerClass
52 LoggerClass.__init__(self, *args, **kwargs)
53 if hasattr (logging,
'_levelNames'):
54 self.__dict__.
update(logging._levelNames)
55 if hasattr (logging,
'_nameToLevel'):
56 self.__dict__.
update(logging._nameToLevel)
57 if hasattr (logging,
'_levelToName'):
58 self.__dict__.
update(logging._levelToName)
61 self.log(VERBOSE_LEVEL, *args)
69 Return the STDOUT handler singleton used for all logging.
71 if "value" in singleton:
72 return singleton[
"value"]
74 handler = logging.StreamHandler()
75 if os.isatty(handler.stream.fileno()):
76 FORMAT =
"[%(asctime)s][$BOLD%(name)-20s$RESET][%(levelname)-18s] %(message)s"
81 log = logging.getLogger()
82 log.addHandler(handler)
83 log.setLevel(logging.DEBUG)
85 singleton[
"value"] = handler
91 A log level context manager. Changes the log level for the duration.
94 old_level = handler.level
96 handler.setLevel(level)
99 handler.setLevel(old_level)
103 log_level = logging.WARNING
105 log_level = logging.INFO
107 log_level = VERBOSE_LEVEL
109 log_level = logging.DEBUG
115 This function should be called only by programs (not by libraries)