[Zope-dev] Log rotation on Windows
Andrew Langmead
alangmead at boston.com
Tue Jan 25 13:59:42 EST 2005
On Jan 24, 2005, at 8:12 PM, Mark Hammond wrote:
> A number of Enfold's customers have requested a reasonable logfile
> rotation
> scheme for Zope on Windows. Enfold would like to work on this and
> contribute it back to Zope. The intention of this mail is to find a
> consensus on the general solution we should adopt, so we can supply
> patches
> with the greatest chance of getting into the core.
Since ZConfig and zLOG are both designed to be extensible. They are
built so that new loggers can be created without any changes to the
Zope core. It seems to me that most of this could be handled by an
add-on package. The ZConfig documentation
<http://svn.zope.org/ZConfig/trunk/doc/zconfig.pdf?rev=440&view=log>
has as its extension example how to add a new logger (a "log to a
pager" logger) to the system. (Of course once this is built, this
add-on package could migrate to the core if there is a compelling need,
either packaging convenience (Windows users don't need to grab an extra
package to manage their systems well.) or so that enhancements to
zLOG's loghandlers can keep the win32 loggers in mind.)
As a rough cut, this will create a logger with a logrotate behavior
similar to what you are looking for.
win32logger/__init__.py:
# empty file to signify a package.
win32logger/component.xml:
<component prefix="win32logger.LogHandlers">
<!-- extend the logging subsystem with a new file logger -->
<import package="zLOG"/>
<sectiontype name="win32-logfile" datatype=".Win32FileHandlerFactory"
implements="zLOG.loghandler" extends="logfile">
<key name="rotate-path" required="yes"/>
</sectiontype>
</component>
win32logger/LogHandlers.py:
import zLOG
import os
class Win32FileHandler(zLOG.LogHandlers.FileHandler):
"""" A file based loghandler that renames on rotate """
def __init__(self, path, rotate_path):
zLOG.LogHandlers.FileHandler.__init__(self,path)
self.rotateFilename = rotate_path
def reopen(self):
self.close()
error = None
try:
os.rename(self.baseFilename, self.rotateFilename)
except OSError, err:
error = err
self.stream = open(self.baseFilename, self.mode)
if error:
zLOG.LOG("Win32Logger", zLOG.ERROR, "Rotate Error", error)
class Win32FileHandlerFactory(zLOG.datatypes.FileHandlerFactory):
def create_loghandler(self):
return
Win32FileHandler(self.section.path,self.section.rotate_path)
and then adjust the zope.conf with:
%import win32logger
<eventlog>
level all
<win32-logfile>
level DEBUG
path $INSTANCE/log/-event.log
rotate-path $INSTANCE/log/event.log.closed
</win32-logfile>
</eventlog>
<logger access>
level WARN
<win32-logfile>
path $INSTANCE/log/Z2.log
rotate-path $INSTANCE/log/Z2.log.closed
</win32-logfile>
</logger>
Of course, it needs to be fleshed out with better error handling, etc.
Although time consuming, maybe the better behavior would be to have the
reopen method copy the current log to the backup file and then seek()
and truncate() the original file. That would take longer to rotate, but
would protect against log files whose close() succeed but open() fails.
More information about the Zope-Dev
mailing list