python日志分割(转)

按文件大小分割:RotatingFileHandler
按时间分割:TimedRotatingFileHandler

import logging
from logging.handlers import TimedRotatingFileHandler
logHandler = TimedRotatingFileHandler(filename="logfile.log", when="S", interval=1, 
        backupCount=3, encoding='utf-8')
logHandler.suffix = "%Y%m%d%H%M"
logFormatter = logging.Formatter('[%(asctime)s %(filename)s:%(lineno)d %(levelname)s] %(message)s')
logHandler.setFormatter(logFormatter)
logger = logging.getLogger('MyLogger')
logger.addHandler(logHandler)
logger.setLevel(logging.INFO)
for k in range(3):                                                    
    logger.error("eror info: %d" % k)

  

 

posted @ 2018-02-06 17:03  hsggj  阅读(346)  评论(0编辑  收藏  举报