Author: tmckay
Date: 2011-04-11 15:06:30 +0000 (Mon, 11 Apr 2011)
New Revision: 4699
Modified:
trunk/parsley/python/parsley/loggingex.py
Log:
Made MyRotatingFileHandler a hybrid between WatchedFileHandler and
RotatingFileHandler. Functionality from WatchedFileHandler reopens
a stream if the file has been renamed since originally opened.
BZ677398
Modified: trunk/parsley/python/parsley/loggingex.py
===================================================================
--- trunk/parsley/python/parsley/loggingex.py 2011-04-08 12:31:19 UTC (rev 4698)
+++ trunk/parsley/python/parsley/loggingex.py 2011-04-11 15:06:30 UTC (rev 4699)
@@ -1,5 +1,6 @@
import logging
import os
+from stat import ST_DEV, ST_INO
import traceback
import select
@@ -32,17 +33,47 @@
class MyRotatingFileHandler(RotatingFileHandler):
def __init__(self, filename, maxBytes, backupCount):
- RotatingFileHandler.__init__(self,
+ RotatingFileHandler.__init__(self,
filename,
maxBytes = maxBytes,
backupCount = backupCount)
+ # Following snippett taken from WatchedFileHandler in Python 2.6.
+ # Essentially, we need a hybrid between a RotatingFileHandler
+ # and a WatchedFileHandler since we may have multiple rotaters all
+ # logging to the same file.
+ if not os.path.exists(self.baseFilename):
+ self.dev, self.ino = -1, -1
+ else:
+ stat = os.stat(self.baseFilename)
+ self.dev, self.ino = stat[ST_DEV], stat[ST_INO]
+ def emit(self, record):
+ # Following snippett taken from WatchedFileHandler in Python 2.6
+ # Reopen the stream if the original file has been moved
+ if not os.path.exists(self.baseFilename):
+ stat = None
+ changed = 1
+ else:
+ stat = os.stat(self.baseFilename)
+ changed = (stat[ST_DEV] != self.dev) or (stat[ST_INO] != self.ino)
+ if changed and self.stream is not None:
+ self.stream.flush()
+ self.stream.close()
+ self.stream = self._open()
+ if stat is None:
+ stat = os.stat(self.baseFilename)
+ self.dev, self.ino = stat[ST_DEV], stat[ST_INO]
+
+ # Now that we've reopened the file if necessary, call the regular
+ # emit() routine for the rotating handler.
+ RotatingFileHandler.emit(self, record)
+
def doRollover(self):
RotatingFileHandler.doRollover(self)
- if hasattr(self, "baseFilename"):
- # Try to fix up permissions on new file
- set_log_owner(self.baseFilename)
+ # Try to fix up permissions on new file
+ set_log_owner(self.baseFilename)
+
def enable_logging(name, level, file_obj, max_MB = 0, max_archives = 0):
assert level
assert file_obj
Show replies by date