-
Notifications
You must be signed in to change notification settings - Fork 9
/
color_log.py
34 lines (26 loc) · 942 Bytes
/
color_log.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
import logging
from termcolor import colored
import colorama
colorama.init()
class ColorLog(object):
colormap = dict(
debug=dict(color='grey', attrs=['bold']),
info=dict(color='green'),
warn=dict(color='yellow', attrs=['bold']),
warning=dict(color='yellow', attrs=['bold']),
error=dict(color='red'),
critical=dict(color='red', attrs=['bold']),
)
def __init__(self, logger):
self._log = logger
def __getattr__(self, name):
if name in ['debug', 'info', 'warn', 'warning', 'error', 'critical']:
return lambda s, *args: getattr(self._log, name)(
colored(s, **self.colormap[name]), *args)
return getattr(self._log, name)
log = ColorLog(logging.getLogger(__name__))
if __name__ == '__main__':
log.setLevel(logging.DEBUG)
stdout = logging.StreamHandler()
stdout.setLevel(logging.DEBUG)
log.addHandler(stdout)