Robert Brewer avatar Robert Brewer committed 327a695

Fix for #660 (Add a 'wsgi.errors' logging handler).

Comments (0)

Files changed (2)

cherrypy/_cplogging.py

         self._set_file_handler(self.access_log, newvalue)
     access_file = property(_get_access_file, _set_access_file,
                            doc="The filename for self.access_log.")
+    
+    
+    # ------------------------- WSGI handlers ------------------------- #
+    
+    def _set_wsgi_handler(self, log, enable):
+        h = self._get_builtin_handler(log, "wsgi")
+        if enable:
+            if not h:
+                h = WSGIErrorHandler()
+                h.setLevel(logging.DEBUG)
+                h.setFormatter(logfmt)
+                h._cpbuiltin = "wsgi"
+                log.addHandler(h)
+        elif h:
+            log.handlers.remove(h)
+    
+    def _get_wsgi(self):
+        return bool(self._get_builtin_handler(self.error_log, "wsgi"))
+    
+    def _set_wsgi(self, newvalue):
+        self._set_wsgi_handler(self.error_log, newvalue)
+    wsgi = property(_get_wsgi, _set_wsgi,
+                      doc="If True, error messages will be sent to wsgi.errors.")
 
+
+class WSGIErrorHandler(logging.Handler):
+    "A handler class which writes logging records to environ['wsgi.errors']."
+    
+    def flush(self):
+        """Flushes the stream."""
+        try:
+            stream = cherrypy.request.wsgi_environ.get('wsgi.errors')
+        except AttributeError, KeyError:
+            pass
+        else:
+            stream.flush()
+    
+    def emit(self, record):
+        """Emit a record."""
+        try:
+            stream = cherrypy.request.wsgi_environ.get('wsgi.errors')
+        except AttributeError, KeyError:
+            pass
+        else:
+            try:
+                msg = self.format(record)
+                fs = "%s\n"
+                import types
+                if not hasattr(types, "UnicodeType"): #if no unicode support...
+                    stream.write(fs % msg)
+                else:
+                    try:
+                        stream.write(fs % msg)
+                    except UnicodeError:
+                        stream.write(fs % msg.encode("UTF-8"))
+                self.flush()
+            except:
+                self.handleError(record)

cherrypy/tutorial/tut01_helloworld.py

     index.exposed = True
 
 
+import os.path
+tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf')
+
 if __name__ == '__main__':
-    import os.path
-    tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf')
-    cherrypy.config.update(tutconf)
     # CherryPy always starts with app.root when trying to map request URIs
     # to objects, so we need to mount a request handler root. A request
     # to '/' will be mapped to HelloWorld().index().
-    cherrypy.quickstart(HelloWorld())
+    cherrypy.quickstart(HelloWorld(), config=tutconf)
 else:
     # This branch is for the test suite; you can ignore it.
-    cherrypy.tree.mount(HelloWorld())
+    cherrypy.tree.mount(HelloWorld(), config=tutconf)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.