1. 程式人生 > >python日誌分割(轉)

python日誌分割(轉)

ati inf val ota cti pos 分割 fix tin

按文件大小分割: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日誌分割(轉)