Ticket #727: 727_logfile.diff
| File 727_logfile.diff, 3.8 KB (added by therve, 7 years ago) |
|---|
-
twisted/python/logfile.py
9 9 """ 10 10 11 11 # System Imports 12 import os, glob, string, time 12 import os, glob, string, time, gzip 13 13 14 14 # sibling imports 15 15 … … 151 151 152 152 threadable.synchronize(LogFile) 153 153 154 class GzipLogFile(LogFile): 155 def rotate(self): 156 if not (os.access(self.directory, os.W_OK) and os.access(self.path, os.W_OK)): 157 return 158 logs = self.listLogs() 159 logs.reverse() 160 for i in logs: 161 os.rename("%s.%d.gz" % (self.path, i), "%s.%d.gz" % (self.path, i + 1)) 162 self._file.close() 154 163 164 newpath = "%s.1" % self.path 165 os.rename(self.path, newpath) 166 gz = gzip.open("%s.gz" % newpath, "wb") 167 newfp = open(newpath) 168 for l in newfp: 169 gz.write(l) 170 gz.close() 171 newfp.close() 172 os.remove(newpath) 173 174 self._openFile() 175 176 def listLogs(self): 177 """Return sorted list of integers - the old logs' identifiers.""" 178 result = [] 179 for name in glob.glob("%s.*" % self.path): 180 try: 181 counter = int(string.split(name, '.')[-2]) 182 if counter: 183 result.append(counter) 184 except ValueError: 185 pass 186 result.sort() 187 return result 188 155 189 class DailyLogFile(BaseLogFile): 156 190 """A log file that is rotated daily (at or after midnight localtime) 157 191 """ -
twisted/test/test_logfile.py
6 6 from twisted.trial import unittest 7 7 8 8 # system imports 9 import os, shutil, time 9 import os, shutil, time, gzip 10 10 11 11 # twisted imports 12 12 from twisted.python import logfile … … 23 23 24 24 def tearDown(self): 25 25 shutil.rmtree(self.dir) 26 pass27 26 28 27 def testWriting(self): 29 28 log = logfile.LogFile(self.name, self.dir) … … 158 157 # reset permission so tearDown won't fail 159 158 os.chmod(self.dir, 0777) 160 159 160 class GzipLogFileTestCase(unittest.TestCase): 161 """Test the compressed rotating log file.""" 162 163 def setUp(self): 164 self.dir = self.mktemp() 165 os.makedirs(self.dir) 166 self.name = "test.log" 167 self.path = os.path.join(self.dir, self.name) 168 169 def tearDown(self): 170 #shutil.rmtree(self.dir) 171 pass 172 173 def testRotation(self): 174 # this logfile should rotate every 10 bytes 175 log = logfile.GzipLogFile(self.name, self.dir, rotateLength=10) 161 176 177 # test automatic rotation 178 log.write("123") 179 log.write("4567890") 180 log.write("1" * 11) 181 self.assert_(os.path.exists("%s.1.gz" % self.path)) 182 g = gzip.open("%s.1.gz" % self.path) 183 self.assertEquals(g.read(), "1234567890") 184 g.close() 185 self.assert_(not os.path.exists("%s.2.gz" % self.path)) 186 log.write('') 187 self.assert_(os.path.exists("%s.1.gz" % self.path)) 188 self.assert_(os.path.exists("%s.2.gz" % self.path)) 189 g = gzip.open("%s.1.gz" % self.path) 190 self.assertEquals(g.read(), "1" * 11) 191 g.close() 192 self.assert_(not os.path.exists("%s.3.gz" % self.path)) 193 log.write("3") 194 self.assert_(not os.path.exists("%s.3.gz" % self.path)) 195 196 # test manual rotation 197 log.rotate() 198 self.assert_(os.path.exists("%s.3.gz" % self.path)) 199 self.assert_(not os.path.exists("%s.4.gz" % self.path)) 200 log.close() 201 202 self.assertEquals(log.listLogs(), [1, 2, 3]) 203 204 162 205 class RiggedDailyLogFile(logfile.DailyLogFile): 163 206 _clock = 0.0 164 207
