3
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
22
COLOR_SEQ = "\033[1;%dm"
25
class ColorFormatter(logging.Formatter):
27
def __init__(self, *args, **kwargs):
28
# can't do super(...) here because Formatter is an old school class
29
logging.Formatter.__init__(self, *args, **kwargs)
31
def format(self, record):
32
levelname = record.levelname
33
color_choice = COLORS[levelname]
35
# A not-so-nice but working way of passing arguments to this formatter
37
color_specified = False
38
for arg in record.args:
39
if isinstance(arg,str) and arg.startswith('$MG'):
43
color_specified = True
44
color_choice = COLORS[elems[2]]
47
record.args = tuple(new_args)
48
color = COLOR_SEQ % (30 + color_choice)
49
message = logging.Formatter.format(self, record) + RESET_SEQ
50
for k,v in COLORS.items():
51
message = message.replace("$" + k, COLOR_SEQ % (v+30))\
52
.replace("$BG" + k, COLOR_SEQ % (v+40))\
53
.replace("$BG-" + k, COLOR_SEQ % (v+40))
56
if levelname == 'INFO':
57
message = message.replace("$RESET", '')\
58
.replace("$BOLD", '')\
59
.replace("$COLOR", color if color_specified else '')
62
message = message.replace("$RESET", RESET_SEQ)\
63
.replace("$BOLD", BOLD_SEQ)\
64
.replace("$COLOR", color)
68
logging.ColorFormatter = ColorFormatter
b'\\ No newline at end of file'