[Twisted-Python] Log rotates not as expected

Pet petshmidt at googlemail.com
Wed Jan 20 05:35:34 MST 2010


Hi,

in code below I try to rotate log files if they reach 5000000 limit,
but this happens only to every 5 files, ls -althr:

-rw-r--r-- 1 root root 977K 2010-01-19 19:03 /var/log/my.log.12
-rw-r--r-- 1 root root 977K 2010-01-19 19:55 /var/log/my.log.10
-rw-r--r-- 1 root root 977K 2010-01-19 20:41 /var/log/my.log.9
-rw-r--r-- 1 root root 977K 2010-01-19 21:30 /var/log/my.log.8
-rw-r--r-- 1 root root 977K 2010-01-19 22:46 /var/log/my.log.7
-rw-r--r-- 1 root root 4.8M 2010-01-20 00:19 /var/log/my.log.11
-rw-r--r-- 1 root root 977K 2010-01-20 00:19 /var/log/my.log.6
-rw-r--r-- 1 root root 977K 2010-01-20 09:17 /var/log/my.log.4
-rw-r--r-- 1 root root 977K 2010-01-20 10:45 /var/log/my.log.3
-rw-r--r-- 1 root root 977K 2010-01-20 12:04 /var/log/my.log.2
-rw-r--r-- 1 root root 977K 2010-01-20 12:59 /var/log/my.log.1
-rw-r--r-- 1 root root 4.2M 2010-01-20 13:26 /var/log/my.log.5
-rw-r--r-- 1 root root 301K 2010-01-20 13:26 /var/log/my.log


it writes simultaneously to both my.log.5 and my.log. It is not big
problem, but in this way I have only recent files, because they grows
quickly

Pet


from twisted.python import log
from twisted.python import logfile
from twisted.application import service

class MyLog(log.FileLogObserver):

    def emit(self, logEntryDict):
        log.FileLogObserver.timeFormat = '%Y-%m-%d %H:%M:%S'
        log.FileLogObserver.emit(self, logEntryDict)


class MyLogService(service.Service):

    def __init__(self, logName, logDir):
        self.logName = logName
        self.logDir = logDir
        # near 5mb
        self.maxLogSize = 5000000

    def startService(self):
        # logfile is a file-like object that supports rotation
        self.logFile = logfile.LogFile(
            self.logName, self.logDir, rotateLength=self.maxLogSize,
maxRotatedFiles=50)
        #self.logFile.rotate() # force rotation each time restarted
        self.loclog = MyLog(self.logFile)
        self.loclog.start()

    def stopService(self):
        self.loclog.stop()
        self.logFile.close()
        del(self.logFile)




More information about the Twisted-Python mailing list