Opened 6 years ago

Closed 6 years ago

#3038 enhancement closed duplicate (duplicate)

Logfile rotation continues infinitely

Reported by: colin Owned by:
Priority: low Milestone:
Component: core Keywords:
Cc: Branch:
Author: Launchpad Bug:

Description

Proposed fix (2.5.0):

--- logfile.py 2008-02-18 09:48:00.000000000 +0200
+++ logfile.py  2008-02-18 09:54:43.000000000 +0200
@@ -90,10 +90,12 @@
     """A log file that can be rotated.
     
     A rotateLength of None disables automatic log rotation.
+    A maxFiles of None disables the limit on the maximum number of files created
     """
-    def __init__(self, name, directory, rotateLength=1000000, defaultMode=None):
+    def __init__(self, name, directory, rotateLength=1000000, defaultMode=None, maxFiles=10):
         BaseLogFile.__init__(self, name, directory, defaultMode)
         self.rotateLength = rotateLength
+        self.maxFiles = maxFiles
 
     def _openFile(self):
         BaseLogFile._openFile(self)
@@ -126,7 +128,8 @@
         logs = self.listLogs()
         logs.reverse()
         for i in logs:
-            os.rename("%s.%d" % (self.path, i), "%s.%d" % (self.path, i + 1))
+            if (not self.maxFiles) or (i < self.maxFiles):
+                os.rename("%s.%d" % (self.path, i), "%s.%d" % (self.path, i + 1))
         self._file.close()
         os.rename(self.path, "%s.1" % self.path)
         self._openFile()

Change History (3)

comment:1 Changed 6 years ago by colin

Unit test for test_logfile.LogFileTestCase

    def testRotationLimit(self):
        # this logfile should rotate every 10 bytes to a limit of 2 files
        log = logfile.LogFile(self.name, self.dir, rotateLength=10, maxFiles=2)

        # test automatic rotation
        log.write("1234567890")
        log.write("1" * 11)
        self.assert_(os.path.exists("%s.1" % self.path))
        self.assert_(not os.path.exists("%s.2" % self.path))
        log.write('')
        self.assert_(os.path.exists("%s.1" % self.path))
        self.assert_(os.path.exists("%s.2" % self.path))
        self.assert_(not os.path.exists("%s.3" % self.path))
        log.write("3")
        self.assert_(not os.path.exists("%s.3" % self.path))

        # test manual rotation
        log.rotate()
        self.assert_(not os.path.exists("%s.3" % self.path))
        log.rotate()
        self.assert_(not os.path.exists("%s.4" % self.path))
        log.close()

        self.assertEquals(log.listLogs(), [1, 2])

comment:2 Changed 6 years ago by therve

  • Resolution set to duplicate
  • Status changed from new to closed

This feature already exists in trunk. The argument is called maxRotatedFiles.

comment:3 Changed 3 years ago by <automation>

  • Owner glyph deleted
Note: See TracTickets for help on using tickets.