[Twisted-Python] Re: Addition to twisted.python.logfile

John Abel john.abel at pa.press.net
Fri Oct 18 05:29:08 MDT 2002


Hi,

Enclosed is an updated logfile.py, with zlib detection - If zlib isn't
imported, compression is turned off.  Please feel to free to
include/tidy up as appropriate.  I've tested the example rotatelog
script with the new logfile.py, on a 10MB file, and it takes around 6-8
seconds to compress, on my laptop (P3-700, 256MB, Mandrake9, Python
2.2.2).

Regards

John

--- /usr/local/lib/python2.2/site-packages/twisted/python/logfile.py
2002-10-18 10:22:17.000000000 +0100
+++
/usr/local/lib/python2.2/site-packages/twisted/python/logfileII.py
2002-10-18 11:57:40.000000000 +0100
@@ -20,7 +20,14 @@
  """

  # System Imports
-import os, stat, glob, string
+import os, stat, glob, string, fileinput, re
+
+try:
+    import gzip
+except ImportError:
+    zlibInstalled = 0
+else:
+    zlibInstalled = 1

  # sibling imports

@@ -34,13 +41,21 @@
      """

      synchronized = ["write", "rotate"]
-

-    def __init__(self, name, directory, rotateLength=1000000):
+
+    def __init__(self, name, directory, rotateLength=1000000,
compressLog=0):
          self.directory = directory
          self.name = name
          self.path = os.path.join(directory, name)
          self.rotateLength = rotateLength
+        if not (compressLog in [0,1] or zlibInstalled):
+            self.compressLog = 0
+        else:
+            self.compressLog = compressLog
+
+        self.globFilePattern = ["%s.*", "%s.*.gz"]
+        self.renameFilePattern = ["%s.%d","%s.%d.gz"]
+        self.rePattern = [".*\.(\d)$",".*\.(\d)\.gz$"]
          self._openFile()

      def _openFile(self):
@@ -87,11 +102,13 @@
      def listLogs(self):
          """Return sorted list of integers - the old logs' identifiers."""
          result = []
-        for name in glob.glob("%s.*" % self.path):
+        for name in glob.glob(self.globFilePattern[self.compressLog] %
self.path):
              try:
-                counter = int(string.split(name, '.')[-1])
-                if counter:
-                    result.append(counter)
+                reCounter = re.match(self.rePattern[self.compressLog],name)
+                if reCounter:
+                    counter = int( reCounter.group(1) )
+                    if counter:
+                        result.append(counter)
              except ValueError:
                  pass
          result.sort()
@@ -108,9 +125,14 @@
          logs = self.listLogs()
          logs.reverse()
          for i in logs:
-            os.rename("%s.%d" % (self.path, i), "%s.%d" % (self.path, i
+ 1))
+            os.rename(self.renameFilePattern[self.compressLog] %
(self.path, i), self.renameFilePattern[self.compressLog] % (self.path, i
+ 1))
          self._file.close()
          os.rename(self.path, "%s.1" % self.path)
+        if self.compressLog:
+            gZippedFile = gzip.GzipFile("%s.1.gz" % self.path,'wb')
+            for LogLine in fileinput.input("%s.1" % self.path):
+                gZippedFile.write(LogLine)
+            gZippedFile.close()
          self._openFile()

      def getCurrentLog(self):
@@ -143,8 +165,6 @@
              line = self._file.readline()
              if not line:
                  break
-            result.append(line)
-        return result
-
      def close(self):
          self._file.close()
+






More information about the Twisted-Python mailing list