Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

"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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...