[Python-Dev] logging and rotation

Xavier Morel python-dev at masklinn.net
Sun Nov 25 18:34:07 CET 2012


On 2012-11-25, at 18:02 , Oleg Broytman wrote:
> On Sun, Nov 25, 2012 at 01:14:11PM +0100, Matthias Bernt <MatatTHC at gmx.de> wrote:
>> I'm using the logging module and write my log messages via the FileHandler.
>> I just realized that using an external log rotation mechanism does not
>> work. That is, new messages are not added to the file after
>> rotation.
> 
>   An external log rotation mechanism ought to send a signal to the
> application and the application ought to close and reopen logs. That is,
> this is an application-level problem, not logging module-level.

I don't know that FileHandler officially supports reopening its
underlying file. On the other hand, WatchedFileHandler[0] does exactly
that and is specifically advertised for use with external log rotators:

> WatchedFileHandler […] watches the file it is logging to. If the file
> changes, it is closed and reopened using the file name.

> A file change can happen because of usage of programs such as newsyslog
> and logrotate which perform log file rotation. […] If the file has changed,
> the old file stream is closed, and the file opened to get a new stream.

[0] http://docs.python.org/2/library/logging.handlers.html#watchedfilehandler


More information about the Python-Dev mailing list