[Zconfig] SVN: ZConfig/trunk/components/logger/loghandler.py * Implement a file handler with 'rotate'. This closes the file, attempts

Sidnei da Silva sidnei at awkly.org
Wed Apr 13 22:22:40 EDT 2005


Log message for revision 29978:
  
    * 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.
  

Changed:
  U   ZConfig/trunk/components/logger/loghandler.py

-=-
Modified: ZConfig/trunk/components/logger/loghandler.py
===================================================================
--- ZConfig/trunk/components/logger/loghandler.py	2005-04-14 02:08:52 UTC (rev 29977)
+++ ZConfig/trunk/components/logger/loghandler.py	2005-04-14 02:22:38 UTC (rev 29978)
@@ -13,7 +13,7 @@
 
 """Handlers which can plug into a PEP 282 logger."""
 
-import os.path
+import os
 import sys
 
 from logging import Handler, StreamHandler
@@ -42,7 +42,28 @@
         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:
+            pass
 
+        self.stream = open(self.baseFilename, self.mode)
+
+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):
     """Handler that does nothing."""
 



More information about the ZConfig mailing list