1. 程式人生 > >python每天一個log檔案 TimedRotatingFileHandler

python每天一個log檔案 TimedRotatingFileHandler

import logging.handlers

log_file = 'test.log'

time_handler = logging.handlers.TimedRotatingFileHandler(log_file, when='D', interval=1, backupCount=0)
time_handler.suffix = '%Y-%m-%d.log'
time_handler.setLevel('ERROR')  # error以上的內容輸出到檔案裡面

fmt = '%(asctime)s - %(funcName)s - %(lineno)s - %(levelname)s - %(message)s'
formatter = logging.Formatter(fmt) time_handler.setFormatter(formatter) logger = logging.getLogger('updateSecurity') logger.setLevel('DEBUG') logger.addHandler(time_handler)