Python TimedRotatingFileHandler在午夜不会旋转,直到将新内容写入日志文件。如何解决这个问题?

时间:2015-09-16 07:03:42

标签: python logging

在24/7运行的烧瓶服务器中,我设置了TimedRotatingFileHandler,以便在每天午夜轮换日志文件。但是,日志轮换会延迟,直到午夜之后将新内容写入日志文件。 如何解决这个问题?

以下是我用于日志轮换的代码。

config = ConfigParser.ConfigParser()
config.read("config.cnf")

#Logging
log_path = config.get('logging', 'log_path')

from logging.handlers import TimedRotatingFileHandler 

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
# add a file handler
fh = logging.handlers.TimedRotatingFileHandler(log_path,when='midnight',interval=1,backupCount=0)
fh.setLevel(logging.DEBUG)
# create a formatter and set the formatter for the handler.
frmt = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
fh.setFormatter(frmt)
# add the Handler to the logger
log.addHandler(fh)

0 个答案:

没有答案