You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.0 KiB

import logging
class Logger(object):
# 日志输出级别
level_relations = {
'debug': logging.DEBUG,
'info': logging.INFO,
'warning': logging.WARNING,
'error': logging.ERROR,
'critical': logging.CRITICAL
}
def __init__(
self,
filename,
level='info',
fmt='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'):
self.logger = logging.getLogger(filename)
# 设置日志格式
format_str = logging.Formatter(fmt)
# 设置日志级别
self.logger.setLevel(self.level_relations.get(level))
# 输出到控制台
sh = logging.StreamHandler()
sh.setFormatter(format_str)
# 输出到文件
th = logging.FileHandler(
filename=filename,
encoding='utf-8',
mode='a')
th.setFormatter(format_str)
self.logger.addHandler(sh)
self.logger.addHandler(th)