-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
39 lines (35 loc) · 1.5 KB
/
logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import copy, logging
import colorful
logger = None
# inspired by
# http://uran198.github.io/en/python/2016/07/12/colorful-python-logging.html
with colorful.with_16_ansi_colors() as c:
LOG_COLORS = {
logging.ERROR: c.red,
logging.WARNING: c.yellow,
logging.DEBUG: c.cyan,
logging.CRITICAL: c.magenta,
logging.INFO: c.green,
}
class ColorFormatter(logging.Formatter):
last_levelno = None
def format(self, record, *args, **kwargs):
with colorful.with_8_ansi_colors() as c:
# if the corresponding logger has children, they may receive modified
# record, so we want to keep it intact
new_record = copy.copy(record)
if new_record.levelno in LOG_COLORS.keys():
levelcolor = LOG_COLORS[new_record.levelno]
# hide level name for repeated log messages with the same level
if new_record.levelno != self.last_levelno:
levelname = new_record.levelname
new_record.levelname = f'{levelcolor}{levelname:<9}{c.reset}'
else:
new_record.levelname = f'{c.reset}{levelcolor} {c.reset}'
self.last_levelno = new_record.levelno
return super(ColorFormatter, self).format(new_record, *args, **kwargs)
formatter = ColorFormatter("%(levelname)s %(message)s")
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger = logging.getLogger(__name__)
logger.addHandler(handler)