[Twisted-Python] startLogging
Steve Freitas
sflist at ihonk.com
Thu Feb 8 13:19:39 EST 2007
On Thu, 2007-02-08 at 12:14 +0000, Martin Evans wrote:
> Are there any samples on how best to interact with logging?
>
> My server needs to log quite a few items, as such the log file is
> growing quite quickly and I would like to be able to switch file
> names on a daily basis. I would also probably then wish to delete
> logs older than 14 days.
>
> I'm not sure how safe it would be to just keep calling startLogging()
> with a different file name mid-session. I realise there has been some
> discussion about this area recently.
Twisted includes a class, twisted.python.logfile.LogFile, which
implements rotation. It does not, however, do auto-deletion. So I did
this on Twisted 2.5 (no idea if 2.4's any different), which saves x old
logs. If you want to discriminate by age, I'd suggest subclassing
logfile.DailyLogFile and using a similar approach.
-------------
import os
from twisted.python import logfile, threadable
class RotatingCleaningLogFile(logfile.LogFile):
""" Deletes old logs. If maxOldLogs<1, doesn't delete old logs."""
def __init__(self, name, directory, maxOldLogs=0,
rotateLength=1000000, defaultMode=None):
logfile.LogFile.__init__(self, name, directory, rotateLength,
defaultMode)
self.maxOldLogs = maxOldLogs
def rotate(self):
logfile.LogFile.rotate(self)
if self.maxOldLogs > 0:
logs = self.listLogs()
for i in logs:
if i > self.maxOldLogs:
os.unlink('%s.%d' % (self.path, i))
threadable.synchronize(RotatingLogFile)
-------------
Good luck!
Steve
More information about the Twisted-Python
mailing list