RotatingFileHandler

按文件大小拆分日志

参数说明

logging.handlers.``RotatingFileHandler(filename, mode=’a’, maxBytes=0, backupCount=0, encoding=None, delay=0)

maxBytes - 每个文件最大字节数,超出时自动添加后缀.1, .2, …
backupCount - 最大备份数

示例

  1. import logging
  2. from logging.handlers import RotatingFileHandler
  3. logger = logging.getLogger('SQD')
  4. logger.setLevel(logging.DEBUG)
  5. formatter = logging.Formatter(
  6. fmt='[%(asctime)s %(levelname)s %(filename)s:%(lineno)d] %(message)s',
  7. datefmt='%Y%m%d %H:%M:%S')
  8. file_hdlr = RotatingFileHandler('log/out.log',
  9. mode='a',
  10. maxBytes=500,
  11. backupCount=3)
  12. file_hdlr.setFormatter(formatter)
  13. logger.addHandler(file_hdlr)
  14. logger.debug('debug message')
  15. logger.info('info message')
  16. logger.warn('warn message')

image.png