Simon Hu
Simon Hu

Reputation: 153

Python: question about the loggind module with TimedRotatingFileHandler

Backgroud: I try to use the loggind module to record my log.

I use TimedRotatingFileHandler and set the date suffix %Y-%m-%d_%H-%M to each log file . However, I noticed that the first log file that be generated by the logging module didn't append the suffix to its file name, how can I fix it or it is inevitable?

file list

Upvotes: 0

Views: 51

Answers (1)

Lenormju
Lenormju

Reputation: 4368

It is simply the name of the current file in which the logger is writing. The others are the previous.
Here is my code :

import logging
import time
from logging import handlers

root_logger = logging.getLogger()
root_logger.addHandler(handlers.TimedRotatingFileHandler("log", when="s"))

for i in range(10):
    root_logger.warning(i)
    time.sleep(0.3)

It produced :

  • log.2022-01-27_17-37-33
    0
    
  • log.2022-01-27_17-37-34
    1
    2
    3
    4
    
  • log.2022-01-27_17-37-35
    5
    6
    7
    8
    
  • log
    9
    

When run again, it renamed log to log.2022-01-27_17-37-36 and produced another 4 files, three with a date and the last without.

Upvotes: 1

Related Questions