I've made a time-rotating logger which creates a new logfile at midnight. In my logfile I want to write a header at the top of every file. I'm looking for an efficient way to call a function which writes this header to the logfile upon the moment that the file is created.
import logging
from logging.handlers import TimedRotatingFileHandler
# create time-rotating log handler
logHandler = TimedRotatingFileHandler(logfile, when='midnight')
# create logger
self.log = logging.getLogger('MyTimeRotatingLogger')
self.log.addHandler(logHandler)
I've solved it! Basically all that needs to be done is overriding the
doRollover
method of theTimedRotatingFileHandler
, also some code is needed in this new parent class in order to pass the logging instance and set the header content. Hereby an extensive example for people who encounter a similar situation.