Reputation: 111
I have a script which runs every day and is using TimedRoatingLogFileHandler of Python. Below is an excerpt from the code.
log = 'test.log' # Set up Log title
filename = '/var/log/' + log
handler = TimedRotatingFileHandler(filename, when="D", interval=1, backupCount=45)
formatter = logging.Formatter('%(asctime)s : %(name)s : %(levelname)s : %(message)s',
datefmt='%a, %d-%b-%Y %H:%M:%S')
handler.setFormatter(formatter)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.addHandler(handler)
logger.info("Script execution started")
I am expecting a new logfile test.log gets created daily and the previous log will be moved to test.log.1 and test.log.2 etc.
Is my assumption correct? If so, why is it not happening? I can see yesterday's output in today's log still. Yesterday's log file is not moved to a different file as I was expecting. I am using Python 2.7 on a SUSE linux machine. I have checked the other threads but most of them don't contain any answers. Please let me know if you need some more details.
Upvotes: 2
Views: 1732
Reputation: 99317
You need to actually log something for rollover to occur. Until your program does this, yesterday's output could still be in the current log.
Update: TimedRotatingFileHandler
is for when the script runs over multiple days, while logging over multiple days. If the script completes every day before the end of the day, you are better off using a FileHandler
with a filename derived from the date.
Upvotes: 2