标签:count this file 常用 warning 文件 str Matter when
日志模块的设置流程
import logging from logging import handlers class IgnoreBackupLogFilter(logging.Filter): def filter(self, record):#固定写法 return ‘db backup‘ not in record.getMessage() #1、生成logger对象 logger = logging.getLogger("web") logger.setLevel(logging.INFO) logger.addFilter(IgnoreBackupLogFilter()) #2、生成handler对象 ch= logging.StreamHandler() #ch.setLevel(logging.INFO) #fh = logging.FileHandler(‘web.log‘) #fh = handlers.RotatingFileHandler(‘web.log‘,maxBytes=10, backupCount=3)#以文件的大小生成日志文件,最多3个 fh = handlers.TimedRotatingFileHandler(‘add.log‘,when=‘S‘,interval=5,backupCount=3)#以5秒为周期生成日志文件,最多3个 #fh.setLevel(logging.WARNING) #2.1把handler对象 绑定到logger logger.addHandler(ch) logger.addHandler(fh) #3、生成formatter对象 file_formatter = logging.Formatter(‘%(asctime)s:%(levelname)s:%(name)s:%(message)s‘) console_formatter =logging.Formatter(‘%(asctime)s:%(levelname)s:%(filename)s:%(lineno)s,%(message)s‘) #3.1把formatter对象绑定到handler ch.setFormatter(console_formatter) fh.setFormatter(file_formatter) logger.debug(‘debug this is‘) logger.warning(‘warning is this‘) logger.info(‘info is this db backup‘) #全局如果不设置级别,默认warning,并且级别优先级最高
标签:count this file 常用 warning 文件 str Matter when
原文地址:https://www.cnblogs.com/yan-long/p/9461867.html