RotatingFileHandler
按文件大小拆分日志
参数说明
logging.handlers.``RotatingFileHandler
(filename, mode=’a’, maxBytes=0, backupCount=0, encoding=None, delay=0)
maxBytes - 每个文件最大字节数,超出时自动添加后缀.1, .2, …
backupCount - 最大备份数
示例
import logging
from logging.handlers import RotatingFileHandler
logger = logging.getLogger('SQD')
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(
fmt='[%(asctime)s %(levelname)s %(filename)s:%(lineno)d] %(message)s',
datefmt='%Y%m%d %H:%M:%S')
file_hdlr = RotatingFileHandler('log/out.log',
mode='a',
maxBytes=500,
backupCount=3)
file_hdlr.setFormatter(formatter)
logger.addHandler(file_hdlr)
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')