Quellcode durchsuchen

Logfile rotation.

Chris McDonough vor 19 Jahren
Ursprung
Commit
2e1787e452
4 geänderte Dateien mit 28 neuen und 22 gelöschten Zeilen
  1. 1 3
      TODO.txt
  2. 1 0
      sample.conf
  3. 16 16
      src/supervisor/options.py
  4. 10 3
      src/supervisor/supervisord.py

+ 1 - 3
TODO.txt

@@ -5,13 +5,11 @@
 
    - provide "restart all" functionality
 
-- Long shutdown time.
-
 - Unit tests for meld classes and ui server.
 
 - Tail main log.
 
-- Test log rotation.
+- Unit tests for log rotation.
 
 - Command-line arg tests.
 

+ 1 - 0
sample.conf

@@ -104,6 +104,7 @@ priority=1
 autostart=true
 autorestart=true
 user=chrism
+logfile_maxbytes=1MB
 
 [program:postgres]
 command=/Users/chrism/projects/websafe/dev-sandbox/bin/postmaster

+ 16 - 16
src/supervisor/options.py

@@ -104,19 +104,19 @@ class RotatingRawFileHandler(RawFileHandler):
         self.maxBytes = maxBytes
         self.backupCount = backupCount
 
-        def emit(self, record):
-            """
-            Emit a record.
-            
-            Output the record to the file, catering for rollover as described
-            in doRollover().
-            """
-            try:
-                if self.shouldRollover(record):
-                    self.doRollover()
-                RawFileHandler.emit(self, record)
-            except:
-                self.handleError(record)
+    def emit(self, record):
+        """
+        Emit a record.
+
+        Output the record to the file, catering for rollover as described
+        in doRollover().
+        """
+        try:
+            if self.shouldRollover(record):
+                self.doRollover()
+            RawFileHandler.emit(self, record)
+        except:
+            self.handleError(record)
 
     def doRollover(self):
         """
@@ -693,9 +693,9 @@ class ServerOptions(Options):
             logfile_backups = config.saneget(section, 'logfile_backups', 10)
             logfile_backups = datatypes.integer(logfile_backups)
             logfile_maxbytes = config.saneget(section, 'logfile_maxbytes',
-                                              datatypes.byte_size('50MB'))
-            logfile_maxbytes = datatypes.integer(logfile_maxbytes)
-            stopsignal = config.saneget(section, 'stopsignal', signal.SIGTERM)
+                                              '50MB')
+            logfile_maxbytes = datatypes.byte_size(logfile_maxbytes)
+            stopsignal = config.saneget(section, 'stopsignal', 'TERM')
             stopsignal = datatypes.signal(stopsignal)
             stopwaitsecs = config.saneget(section, 'stopwaitsecs', 10)
             stopwaitsecs = datatypes.integer(stopwaitsecs)

+ 10 - 3
src/supervisor/supervisord.py

@@ -50,6 +50,7 @@ import asyncore
 import traceback
 import StringIO
 import shlex
+import logging
 
 from options import ServerOptions
 from options import decode_wait_status
@@ -108,10 +109,16 @@ class Subprocess:
         self.config = config
         self.pipes = {}
         if config.logfile:
-            self.childlog = options.getLogger(config.logfile, 10,
+            backups = config.logfile_backups
+            maxbytes = config.logfile_maxbytes
+            # using "not not maxbytes" below is an optimization.  If
+            # maxbytes is zero, it means we're not using rotation.  The
+            # rotating logger is more expensive than the normal one.
+            self.childlog = options.getLogger(config.logfile, logging.INFO,
                                               '%(message)s',
-                                              config.logfile_backups,
-                                              config.logfile_maxbytes)
+                                              rotating=not not maxbytes,
+                                              maxbytes=maxbytes,
+                                              backups=backups)
 
     def removelogs(self):
         if self.childlog: