Pārlūkot izejas kodu

Extract method for setting up the main log

Jason R. Coombs 12 gadi atpakaļ
vecāks
revīzija
5deef79c70
1 mainītis faili ar 25 papildinājumiem un 16 dzēšanām
  1. 25 16
      supervisor/dispatchers.py

+ 25 - 16
supervisor/dispatchers.py

@@ -91,24 +91,10 @@ class POutputDispatcher(PDispatcher):
         self.fd = fd
         self.channel = channel = self.event_type.channel
 
-        logfile = getattr(process.config, '%s_logfile' % channel)
+        self._setup_logging(process.config, channel)
+
         capture_maxbytes = getattr(process.config,
                                    '%s_capture_maxbytes' % channel)
-
-        if logfile:
-            maxbytes = getattr(process.config, '%s_logfile_maxbytes' % channel)
-            backups = getattr(process.config, '%s_logfile_backups' % channel)
-            fmt = '%(message)s'
-            if logfile == 'syslog':
-                fmt = ' '.join((process.config.name, fmt))
-            self.mainlog = process.config.options.getLogger(
-                logfile,
-                loggers.LevelsByName.INFO,
-                fmt=fmt,
-                rotating=not not maxbytes, # optimization
-                maxbytes=maxbytes,
-                backups=backups)
-
         if capture_maxbytes:
             self.capturelog = self.process.config.options.getLogger(
                 None, # BoundIO
@@ -131,6 +117,29 @@ class POutputDispatcher(PDispatcher):
         self.stdout_events_enabled = config.stdout_events_enabled
         self.stderr_events_enabled = config.stderr_events_enabled
 
+    def _setup_logging(self, config, channel):
+        """
+        Configure the main log according to the process' configuration and
+        channel. Sets `mainlog` on self. Returns nothing.
+        """
+
+        logfile = getattr(config, '%s_logfile' % channel)
+        if not logfile:
+            return
+
+        maxbytes = getattr(config, '%s_logfile_maxbytes' % channel)
+        backups = getattr(config, '%s_logfile_backups' % channel)
+        fmt = '%(message)s'
+        if logfile == 'syslog':
+            fmt = ' '.join((config.name, fmt))
+        self.mainlog = config.options.getLogger(
+            logfile,
+            loggers.LevelsByName.INFO,
+            fmt=fmt,
+            rotating=not not maxbytes, # optimization
+            maxbytes=maxbytes,
+            backups=backups)
+
     def removelogs(self):
         for log in (self.mainlog, self.capturelog):
             if log is not None: