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)

你可能感兴趣的:(python日志分割)