How to force a rotating name with python's TimedRotatingFileHandler?

前端 未结 3 704
面向向阳花
面向向阳花 2021-01-19 00:20

I am trying to use TimedRotatingFileHandler to keep daily logs in separate log files. The rotation works perfectly as intended, but what I don\'t like how it does is the nam

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-19 00:57

    As far as I know there is no way to directly achieve this.

    One solution you could try is to override the default behavior.

    • Create your own TimedRotatingFileHandler class and override the doRollover() function.
    • Check the source in your python installation /Lib/logging/handlers.py

    Something like this:

    class MyTimedRotatingFileHandler(TimedRotatingFileHandler):
        def __init__(self, **kwargs):
            TimedRotatingFileHandler.__init__(self, **kwargs)
    
        def doRollover(self):
            # Do your stuff, rename the file as you want 
    

提交回复
热议问题