2

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.

Goutham Anush
  • 101
  • 1
  • 12

1 Answers1

2

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.

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
  • Please see the last line. I have included a "logger.info" statement at the end. And as i have said, this is just an excerpt. A lot more is there in the script and hence lot more logger.info or logger.error statements. – Goutham Anush Jul 28 '17 at 16:25