Reputation: 2995
I'm using the logging
module python 3.4 to log performance over many days. I wanted to have it rotate so that everyday it creates and writes to a new log file with the date in the name. The logging
module's TimedRotatingFileHandler
handler does that well enough. However, say I put in a log filename 'FN.log' and I get:
FN.log
FN.log.2016.05.03
FN.log.2016.05.04
FN.log.2016.05.05
etc..
Is there a way to have the first file also include the date? I've also considered maybe immediately triggering a rotation on startup which should do the trick but I don't know how to do that yet. Thanks!
PS: Ideally, if I happen to kill the app and restart it on the same day, it would continue writing in the same log file as the pre-existing one from that day (the one created by the rotation, for instance). However that last bit is optional: if it's too hard, I can always create a new log directory everytime I run the program.
Upvotes: 3
Views: 1938
Reputation: 99307
It's not the first file - it's the current file being logged to. When rollover happens, the file FN.log
gets renamed with a name containing a date, and a brand new FN.log
file is opened and used for subsequent logs until the next rollover.
Upvotes: 11