blob: d885ea99f1531ffbb2b41882f86c41bd44d958be [file] [log] [blame]
koder aka kdanilov962ee5f2016-12-19 02:40:08 +02001import yaml
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +03002import logging
koder aka kdanilov962ee5f2016-12-19 02:40:08 +02003import logging.config
koder aka kdanilov73084622016-11-16 21:51:08 +02004from typing import Callable, IO, Optional
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +03005
6
koder aka kdanilov22d134e2016-11-08 11:33:19 +02007def color_me(color: int) -> Callable[[str], str]:
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +03008 RESET_SEQ = "\033[0m"
9 COLOR_SEQ = "\033[1;%dm"
10
11 color_seq = COLOR_SEQ % (30 + color)
12
13 def closure(msg):
14 return color_seq + msg + RESET_SEQ
15 return closure
16
17
18class ColoredFormatter(logging.Formatter):
19 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
20
21 colors = {
22 'WARNING': color_me(YELLOW),
23 'DEBUG': color_me(BLUE),
24 'CRITICAL': color_me(YELLOW),
25 'ERROR': color_me(RED)
26 }
27
koder aka kdanilov22d134e2016-11-08 11:33:19 +020028 def __init__(self, msg: str, use_color: bool=True, datefmt: str=None) -> None:
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030029 logging.Formatter.__init__(self, msg, datefmt=datefmt)
30 self.use_color = use_color
31
koder aka kdanilov22d134e2016-11-08 11:33:19 +020032 def format(self, record: logging.LogRecord) -> str:
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030033 orig = record.__dict__
34 record.__dict__ = record.__dict__.copy()
35 levelname = record.levelname
36
37 prn_name = levelname + ' ' * (8 - len(levelname))
38 if levelname in self.colors:
39 record.levelname = self.colors[levelname](prn_name)
40 else:
41 record.levelname = prn_name
42
43 # super doesn't work here in 2.6 O_o
44 res = logging.Formatter.format(self, record)
45
46 # res = super(ColoredFormatter, self).format(record)
47
48 # restore record, as it will be used by other formatters
49 record.__dict__ = orig
50 return res