pythonlogging

TimedRotatingFileHandler Changing File Name?


I am trying to implement the python logging handler TimedRotatingFileHandler.

When it rolls over to midnight it appends the current day in the form YYYY-MM-DD.

LOGGING_MSG_FORMAT  = '%(name)-14s > [%(levelname)s] [%(asctime)s] : %(message)s'
LOGGING_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'

logging.basicConfig(
            level=logging.DEBUG,
            format=LOGGING_MSG_FORMAT,
            datefmt=LOGGING_DATE_FORMAT
            )
root_logger = logging.getLogger('')
logger = logging.handlers.TimedRotatingFileHandler("C:\\logs\\Rotate_Test",'midnight',1)
root_logger.addHandler(logger)
while True:
    daemon_logger = logging.getLogger('TEST')
    daemon_logger.info("SDFKLDSKLFFJKLSDD")
    time.sleep(60)

The first log file created is named Rotate_Test, then once it rolls over to the next day it changes the file name to Rotate_Test.YYYY-MM-DD where YYYY-MM-DD is the current day.

How can I change how it alters the filename?


Solution

  • "How can i change how it alters the filename?"

    Since it isn't documented, I elected to read the source. This is what I concluded from reading the source of logging/handlers.py

    handler = logging.handlers.TimedRotatingFileHandler("C:\\isis_ops\\logs\\Rotate_Test",'midnight',1)
    handler.suffix = "%Y-%m-%d" # or anything else that strftime will allow
    root_logger.addHandler(handler)
    

    The suffix is the formatting string.