-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging_config.py
50 lines (44 loc) · 1.43 KB
/
logging_config.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 os
import logging.config
from pathlib import Path
from config import ROOTDIR
if not os.path.exists(ROOTDIR / 'data'):
os.mkdir(ROOTDIR / 'data')
LOG_FILE = Path(ROOTDIR) / 'data/logfile.log'
def setup_logging(fname=LOG_FILE):
no_color = '\33[m'
red, green, orange, blue, purple, lblue, grey = (
map('\33[%dm'.__mod__, range(31, 38)))
logging_config = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'default': {
'format': '[%(asctime)s] %(levelname)-4s - '
'%(name)-4s - %(message)s'
},
'color': {
'format': '{}[%(asctime)s]{} {}%(levelname)-5s{} - '
'{}%(name)-5s{}: %(message)s'.format(green, no_color, purple, no_color, orange, no_color)
}
},
'handlers': {
'stream': {
'class': 'logging.StreamHandler',
'formatter': 'color',
}
},
'root': {
'handlers': ['stream'],
'level': logging.INFO,
},
}
if fname is not None:
logging_config['handlers']['file'] = {
'class': 'logging.FileHandler',
'formatter': 'default',
'level': logging.DEBUG,
'filename': fname,
}
logging_config['root']['handlers'].append('file')
logging.config.dictConfig(logging_config)