[Zodb-checkins] CVS: Packages/zLOG - LogHandlers.py:1.6.14.3

Sidnei da Silva sidnei at awkly.org
Tue Apr 12 22:59:37 EDT 2005


Update of /cvs-repository/Packages/zLOG
In directory cvs.zope.org:/tmp/cvs-serv16249

Modified Files:
      Tag: Zope-2_7-branch
	LogHandlers.py 
Log Message:

* Implement a file handler with 'rotate'.  This closes the file, attempts
  a rename to {filename}.last, then reopens the original name.  This
  then makes the log available for another process to perform the rotation
  logic on. This log handler is only installed on the Windows
  platform, where you can't rename an open file.


=== Packages/zLOG/LogHandlers.py 1.6.14.2 => 1.6.14.3 ===
--- Packages/zLOG/LogHandlers.py:1.6.14.2	Thu Oct  2 16:25:53 2003
+++ Packages/zLOG/LogHandlers.py	Tue Apr 12 22:59:36 2005
@@ -13,11 +13,13 @@
 """Handlers which can plug into a PEP 282 logger."""
 
 import sys
+import os
 
 from logging import Handler, StreamHandler
 from logging.handlers import SysLogHandler
 from logging.handlers import HTTPHandler, SMTPHandler
 from logging.handlers import NTEventLogHandler as Win32EventLogHandler
+import zLOG
 
 class FileHandler(StreamHandler):
     """
@@ -35,6 +37,42 @@
     def reopen(self):
         self.close()
         self.stream = open(self.baseFilename, self.mode)
+
+class Win32FileHandler(FileHandler):
+    """File-based log handler for Windows that supports an additional 'rotate'
+    method.  reopen() is generally useless since Windows cannot do a move on
+    an open file.
+    """
+    def rotate(self, rotateFilename=None):
+        if not rotateFilename:
+            rotateFilename = self.baseFilename + ".last"
+        error = None
+        self.close()
+        try:
+            os.rename(self.baseFilename, rotateFilename)
+        except OSError, error:
+            # logger cannot be used until we get a file handle again, so we
+            # store it in 'error'
+            pass
+
+        self.stream = open(self.baseFilename, self.mode)
+
+        if error:
+            # Note we *expect* to see a "file exists" error if some external 
+            # process has not cleaned up these logs.  For now, that just gets
+            # logged like any other error.
+            zLOG.LOG("Win32FileLogger", zLOG.ERROR,
+                     "File rotation error moving '%s' to '%s':" %
+                               (self.baseFilename, rotateFilename), error)
+        else:
+            zLOG.LOG("Win32FileLogger", zLOG.INFO,
+                     "File rotation moved '%s' to '%s'" %
+                               (self.baseFilename, rotateFilename))
+
+if os.name == "nt":
+    # Make it the default for Windows - we install a 'reopen' handler that
+    # tries to rotate the logfile.
+    FileHandler = Win32FileHandler
 
 class NullHandler(Handler):
     """



More information about the Zodb-checkins mailing list