简体   繁体   中英

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?

文件列表

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM