Created
March 16, 2010 21:29
-
-
Save williamsjj/334545 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rotate(self): | |
""" | |
Rotate the file and create a new one. | |
If it's not possible to open new logfile, this will fail silently, | |
and continue logging to old logfile. | |
""" | |
def rotateOldLogs(): | |
""" | |
Rotate log files that aren't the current, and remove any | |
that exceed maxRotatedFiles. | |
""" | |
if self.rotationInProgress: | |
return False | |
self.rotationInProgress = True | |
if not (os.access(self.directory, os.W_OK) and os.access(self.path, os.W_OK)): | |
self.rotationInProgress = False | |
return False | |
logs = self.listLogs() | |
logs.reverse() | |
for i in logs: | |
if self.maxRotatedFiles is not None and i >= self.maxRotatedFiles: | |
os.remove("%s.%d" % (self.path, i)) | |
else: | |
os.rename("%s.%d" % (self.path, i), "%s.%d" % (self.path, i + 1)) | |
return True | |
def cb_rotateCurrLog(rotate_old_logs_success): | |
""" | |
Rotate the currently open log file | |
""" | |
if not rotate_old_logs_success: | |
return | |
self._file.close() | |
os.rename(self.path, "%s.1" % self.path) | |
self._openFile() | |
self.rotationInProgress = False | |
def eb_rotateFailed(failure): | |
""" | |
Rotation failed. Consume the error. | |
""" | |
self.rotationInProgress = False | |
failure.trap(OSError, IOError) | |
return threads.deferToThread(rotateOldLogs).addCallback(cb_rotateCurrLog | |
.addErrback(eb_rotateFailed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment