[Python-Dev] PEP 282 Implementation

Guido van Rossum guido@python.org
Tue, 24 Sep 2002 11:37:49 -0400


> Chris McDonough wrote:
> > It would be helpful for the FileHandler class to define a method
> > which just closes and reopens the current logfile (instead of
> > actually rotating a set like-named logfiles).  This would allow
> > logfile rotation to be performed by a separate process (e.g.
> > RedHat's logrotate).  Sometimes it's better (and even necessary) to
> > be able to use system-provided log rotation facilities instead of
> > relying on the native rotation facilities.
> 
> I'm not sure whether this should be in the core functionality. I
> presume you don't mean an atomic "close and reopen" operation -
> rather, are you suggesting close the file, maybe rename it at the
> application level, then reopen? If so, then it's best handled
> entirely in the application level, through a subclass of
> FileHandler. This allows each application to consider issues such as
> what to do with events that occur between close and reopen (e.g. if
> multiple threads are running).

No, this is using Unix functionality where once you have opened a
file, if the file is renamed, you can continue to write to it and you
will be writing to the renamed file.  IOW the open file is connected
to the inode, not the filename.

Typically an application catches SIGHUP (though that has its share of
problems!) and in response simply closes and reopens the file, using
the original filename.  The sysadmin uses this as follows:

  mv foo.log foo.log.1
  kill -HUP `cat foo.pid`

Having looked at it again, I think that this is definitely better than
doing log rotation in the FileHandler.  The rotation code in the log
handler currently calls tell() after each record is emitted.  This is
expensive, and not needed if you use an external process to watch over
the log files and rotate them.

--Guido van Rossum (home page: http://www.python.org/~guido/)