-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.py
65 lines (56 loc) · 1.87 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import logging # 引入logging模块
import os
import time
import defaults
#
# import configparser
#
# cf = configparser.ConfigParser().read('defaults.py')
SAVE_LOG = defaults.SAVE_LOG
LEVEL = defaults.LOG_LEVEL
maps = {
'DEBUG': logging.DEBUG,
'INFO': logging.INFO,
'WARNING': logging.WARNING,
'CRITICAL': logging.CRITICAL,
}
# def set_name(name):
# return name
if not SAVE_LOG:
def Logger(set_name=None):
logging.basicConfig(level=maps.get(LEVEL),
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
logger = logging
return logger
else:
def Logger(set_name=None):
# 第一步,创建一个logger
logger = logging.getLogger()
logger.setLevel(maps.get(LEVEL)) # Log等级总开关
# 第二步,创建一个handler,用于写入日志文件
tm = time.strftime('%Y_%m_%d__%H_%M', time.localtime(time.time()))
log_path = defaults.LOG_PATH
log_name = defaults.LOG_NAME % {'name': set_name, 'time': tm}
logfile = os.path.join(log_path, log_name)
print(logfile)
fh = logging.FileHandler(logfile, mode='w')
fh.setLevel(maps.get(LEVEL)) # 输出到file的log等级的开关
# 第三步,定义handler的输出格式
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(formatter)
# 第四步,将logger添加到handler里面
logger.addHandler(fh)
return logger
# class Logger(object):
#
# def __init__(self):
#
# self.level = defaults.LOG_LEVEL
# self.save_log =defaults.SAVE_LOG
#
# def __new__(cls, *args, **kwargs):
#
# return super(Logger,cls).__new__(cls, *args, **kwargs)
if __name__ == '__main__':
logger = Logger()
logger.info('fasdddfa')