-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlog.py
30 lines (26 loc) · 1.01 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
#encoding = utf-8
import logging
class Logger():
def __init__(self, logname, loglevel=logging.DEBUG, loggername=None):
'''
指定保存日志的文件路径,日志级别,以及调用文件
将日志存入到指定的文件中
'''
# 创建一个logger
self.logger = logging.getLogger(loggername)
self.logger.setLevel(loglevel)
# 创建一个handler,用于写入日志文件
fh = logging.FileHandler(logname)
fh.setLevel(loglevel)
if not self.logger.handlers:
# 再创建一个handler,用于输出到控制台
ch = logging.StreamHandler()
ch.setLevel(loglevel)
formatter = logging.Formatter(
'[%(levelname)s]%(asctime)s %(filename)s:%(lineno)d: %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
self.logger.addHandler(fh)
self.logger.addHandler(ch)
def getlog(self):
return self.logger