-
Notifications
You must be signed in to change notification settings - Fork 13
/
log.py
42 lines (32 loc) · 1.05 KB
/
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
35
36
37
38
39
40
41
42
import logging
import time,os,sys
if sys.version_info.major==3:
import configparser as cfg
else:
import ConfigParser as cfg
class log(object):
# root logger setting
save_path = time.strftime("%m_%d_%H_%M") + '.log'
l = logging.getLogger()
l.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
# clear handler streams
for it in l.handlers:
l.removeHandler(it)
# file handler setting
config = cfg.RawConfigParser()
config.read('util.config')
save_dir = config.get('general', 'log_path')
if not os.path.exists(save_dir):
os.makedirs(save_dir)
save_path = os.path.join(save_dir, save_path)
f_handler = logging.FileHandler(save_path)
f_handler.setLevel(logging.DEBUG)
f_handler.setFormatter(formatter)
# console handler
c_handler = logging.StreamHandler()
c_handler.setLevel(logging.INFO)
c_handler.setFormatter(formatter)
l.addHandler(f_handler)
l.addHandler(c_handler)
# print(l.handlers[0].__dict__)