写一个logger的类
参考:https://www.cnblogs.com/yuanchenqi/articles/5732581.html
写一个logger的类
import logging
#建立日志对象
class logger():
def __init__(self,logPath):
# 制定log格式
format = logging.Formatter(
'%(asctime)s %(filename)s %(name)s [line:%(lineno)d] %(message)s')
self.logger=logging.getLogger()
#建立文件对象,默认文件写入模式是“a”
fh=logging.FileHandler(logPath,'a')
#设计logger等级
self.logger.setLevel(logging.DEBUG)
#创建屏幕打印对象
sh=logging.StreamHandler()
#添加格式
fh.setFormatter(format)
sh.setFormatter(format)
#logger对象可以添加多个fh和ch对象
self.logger.addHandler(fh)
self.logger.addHandler(sh)
# def logger_level():
# self.logger.debug('logger debug message1')
# self.logger.info('logger info message1')
# self.logger.warning('logger warning message1')
# self.logger.error('logger error message1')
# self.logger.critical('logger critical message1')
def add(self,a,b):
return a+b
logpath=os.path.join(logPath,'9999')
m=logger(logpath)
m.logger.debug('9999')
浙公网安备 33010602011771号