-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
50 lines (40 loc) · 1.38 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
40
41
42
43
44
45
46
47
48
49
50
import logging
import sys
from copy import copy
MAPPING = {
'DEBUG': 37, # white
'INFO': 36, # cyan
'WARNING': 33, # yellow
'ERROR': 31, # red
'CRITICAL': 41, # white on red bg
}
PREFIX = '\033['
SUFFIX = '\033[0m'
class ColoredFormatter(logging.Formatter):
def __init__(self, patern):
logging.Formatter.__init__(self, patern)
def format(self, record):
colored_record = copy(record)
levelname = colored_record.levelname
seq = MAPPING.get(levelname, 37) # default white
colored_levelname = ('{0}{1}m{2}{3}') \
.format(PREFIX, seq, levelname, SUFFIX)
colored_record.levelname = colored_levelname
return logging.Formatter.format(self, colored_record)
rootlog = logging.getLogger()
rootlog.setLevel(logging.DEBUG)
logging.root.manager.loggerDict["urllib3.connectionpool"] \
.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
handler.setFormatter(formatter)
cf = ColoredFormatter(
"[%(name)s][%(levelname)s] %(message)s (%(filename)s:%(lineno)d)"
)
handler.setFormatter(cf)
rootlog.addHandler(handler)
# .setFormatter(formatter))
# logging.root.manager.loggerDict["urllib3.connectionpool"].addHandler(handler.setFormatter(cf))