1

I'm using the RotatingFileHandler (https://docs.python.org/2/library/logging.handlers.html) in Python to log messages - is there anyway to do a function callback when the RotatingFileHandler switches to a new file?

For example, I'm logging messages, and when the file rotates I want to process all of the messages using another defined function

Thanks!

1 Answer 1

4

There's not really a callback, but you can easily subclass the RotatingFileHandler and override your own "rotation", by implementing the doRollover method:

MyFileHandler(RotatingFileHandler):

    def doRollover():
        # invoke the superclass' actual rotation implementation
        super(MyFileHandler, self).doRollover()

        # start doing your own tasks
        # ...
Sign up to request clarification or add additional context in comments.

2 Comments

that's a great solution! is there a way to get the file handle for the last closed file after the rollover?
I'm pretty sure - though I don't really know the details of the RotatingFileHandler implementation. Have a look at the actual implementation: hg.python.org/cpython/file/23ab1197df0b/Lib/logging/… that may help...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.