Commits

Ilya Sukhanov committed d9258a0 Draft

Fix issue #106: pywsgi should use logging module

Comments (0)

Files changed (2)

+# HG changeset patch
+# Parent 68b40aa5b576a679cb41562499092038acb02c78
+Fix issue #106: pywsgi should use logging module
+
+diff -r 68b40aa5b576 -r f20439e4a8e2 gevent/pywsgi.py
+--- a/gevent/pywsgi.py	Thu Aug 30 17:41:32 2012 +0400
++++ b/gevent/pywsgi.py	Thu Aug 30 10:16:23 2012 -0400
+@@ -8,6 +8,7 @@
+ import mimetools
+ from datetime import datetime
+ from urllib import unquote
++import logging
+ 
+ from gevent import socket
+ import gevent
+@@ -275,7 +276,10 @@
+         except Exception:
+             pass
+         try:
+-            sys.stderr.write(message + '\n')
++            try:
++                self.server.log.write(message + '\n')
++            except AttributeError:
++                self.server.log.error(message)
+         except Exception:
+             traceback.print_exc()
+ 
+@@ -421,10 +425,21 @@
+     def log_request(self):
+         log = self.server.log
+         if log:
+-            log.write(self.format_request() + '\n')
++            message=self.format_request()
++            try:
++                try:
++                    self.server.log.write(message + '\n')
++                except AttributeError:
++                    self.server.log.info(message)
++            except Exception:
++                traceback.print_exc()
+ 
+     def format_request(self):
+-        now = datetime.now().replace(microsecond=0)
++        now = ""
++        log = self.server.log
++        if isinstance(log, file):
++            now = datetime.now().replace(microsecond=0)
++            now = " [%s] " % now
+         if self.time_finish:
+             delta = '%.6f' % (self.time_finish - self.time_start)
+             length = self.response_length
+@@ -432,7 +447,7 @@
+             delta = '-'
+             if not self.response_length:
+                 length = '-'
+-        return '%s - - [%s] "%s" %s %s %s' % (
++        return '%s - -%s"%s" %s %s %s' % (
+             self.client_address[0],
+             now,
+             self.requestline,
+@@ -548,16 +563,18 @@
+                 'wsgi.multiprocess': False,
+                 'wsgi.run_once': False}
+ 
+-    def __init__(self, listener, application=None, backlog=None, spawn='default', log='default', handler_class=None,
++    def __init__(self, listener, application=None, backlog=None, spawn='default', log=None, handler_class=None,
+                  environ=None, **ssl_args):
+         StreamServer.__init__(self, listener, backlog=backlog, spawn=spawn, **ssl_args)
+         if application is not None:
+             self.application = application
+         if handler_class is not None:
+             self.handler_class = handler_class
+-        if log == 'default':
+-            self.log = sys.stderr
+-        else:
++        if log is None:
++            self.log = logging.getLogger()
++        elif isinstance(log, str):
++            self.log = logging.getLogger(log)
++        elif isinstance(log, file):
+             self.log = log
+         self.set_environ(environ)
+         self.set_max_accept()
+issue106
 # Placed by Bitbucket