In the flask server which runs 24/7, I set up the TimedRotatingFileHandler to rotate the log file at everyday midnight. However the log rotation is delayed till something new is written to the log file after the midnight. How to fix this issue?
Here is the code I've used for log rotation.
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)