[Twisted-Python] startLogging
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. Thanks, Martin
On 2/8/07, Martin Evans <martin@browns.co.uk> 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.
From a quick look at log.py it does not appear that calling startLogging again would stop logging to the old, it would just add another log observer, meaning that log entries would go to both files.
One approach is to create your own log observer and using this to log with. Then you can change file whenever you want to, and also change the log layout etc.. Alternatively you can call removeObserver and addObserver when you want to change log file. Another approach is to use an external program to rotate the log files. -- - Henrik
Le jeudi 08 février 2007 à 12:14 +0000, Martin Evans a écrit :
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.
You could use Python's standard logging module, it has a TimedRotatingFileHandler which seems to do exactly what you want: http://docs.python.org/lib/node414.html Regards
Martin Evans wrote:
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.
Assuming your platform has a [decent] syslogd, how about logging to syslog, instead of reimplementing parts of it? Something like this should work: % twistd --syslog --prefix foo -oy foo.tac And then let syslog do the hard work. -Mark
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
On Thu, 08 Feb 2007 10:19:39 -0800, Steve Freitas <sflist@ihonk.com> wrote:
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.
FWIW, there's a patch which adds similar functionality in the tracker (or maybe it has been applied to trunk by now, I can't remember). So in the next Twisted release this will probably be available, although it is not yet clear how it will be exposed via the twistd commandline. Jean-Paul
participants (6)
-
Antoine Pitrou -
Henrik Thostrup Jensen -
Jean-Paul Calderone -
Mark Leonard -
Martin Evans -
Steve Freitas