-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.py
executable file
·97 lines (71 loc) · 2.58 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os, logging, sys
import logging.handlers
from logging import raiseExceptions, Logger
LOG_PATH = os.getcwd()+'/logging'
class AppLogger(Logger):
"""
自定义logger
如果handler名称为console表示在终端打印所有大于等于设置级别的日志
其他handler则只记录等于设置级别的日志
"""
def __init__(self, name, level=logging.NOTSET):
super(AppLogger, self).__init__(name, level)
def callHandlers(self, record):
c = self
found = 0
while c:
for hdlr in c.handlers:
found = found + 1
if hdlr.name == 'console':
if record.levelno >= hdlr.level:
hdlr.handle(record)
else:
if record.levelno == hdlr.level:
hdlr.handle(record)
if not c.propagate:
c = None
else:
c = c.parent
if (found == 0) and raiseExceptions and not self.manager.emittedNoHandlerWarning:
sys.stderr.write("No handlers could be found for logger \"%s\"\n" % self.name)
self.manager.emittedNoHandlerWarning = 1
def get_logger(logfile_name=__name__, log_path=LOG_PATH):
'''
save log to diffrent file by deffirent log level into the log path
and print all log in console
'''
logging.setLoggerClass(AppLogger)
formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(lineno)s %(message)s', '%Y-%m-%d %H:%M:%S')
log_files = {
logging.DEBUG: os.path.join(log_path, logfile_name + '-debug.log'),\
logging.INFO: os.path.join(log_path, logfile_name + '-info.log'),\
logging.WARNING: os.path.join(log_path, logfile_name + '-warning.log'),\
logging.ERROR: os.path.join(log_path, logfile_name + '-error.log'),\
logging.CRITICAL:os.path.join(log_path, logfile_name + '-critical.log')\
}
# 和flask默认使用同一个logger
logger = logging.getLogger('werkzeug')
logger.setLevel(logging.DEBUG)
for log_level, log_file in log_files.items():
file_handler = logging.handlers.TimedRotatingFileHandler(log_file,'midnight')
file_handler.setLevel(log_level)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
#控制台显示
console_handler = logging.StreamHandler()
console_handler.name = "console"
console_handler.setLevel(logging.DEBUG)
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
return logger
logger = get_logger()
# handler = logging.FileHandler('loggings.log', encoding='UTF-8')
# logging_format = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s')
# handler.setFormatter(logging_format)
# app.logger.addHandler(handler)
"""
logger.debug('----')
logger.info('----')
logger.error('----')
logger.warning('----')
"""