Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1619

Show
Ignore:
Timestamp:
02/16/07 14:45:21
Author:
fumanchu
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/_cplogging.py

    r1615 r1619  
    152152    access_file = property(_get_access_file, _set_access_file, 
    153153                           doc="The filename for self.access_log.") 
    154  
     154     
     155     
     156    # ------------------------- WSGI handlers ------------------------- # 
     157     
     158    def _set_wsgi_handler(self, log, enable): 
     159        h = self._get_builtin_handler(log, "wsgi") 
     160        if enable: 
     161            if not h: 
     162                h = WSGIErrorHandler() 
     163                h.setLevel(logging.DEBUG) 
     164                h.setFormatter(logfmt) 
     165                h._cpbuiltin = "wsgi" 
     166                log.addHandler(h) 
     167        elif h: 
     168            log.handlers.remove(h) 
     169     
     170    def _get_wsgi(self): 
     171        return bool(self._get_builtin_handler(self.error_log, "wsgi")) 
     172     
     173    def _set_wsgi(self, newvalue): 
     174        self._set_wsgi_handler(self.error_log, newvalue) 
     175    wsgi = property(_get_wsgi, _set_wsgi, 
     176                      doc="If True, error messages will be sent to wsgi.errors.") 
     177 
     178 
     179class WSGIErrorHandler(logging.Handler): 
     180    "A handler class which writes logging records to environ['wsgi.errors']." 
     181     
     182    def flush(self): 
     183        """Flushes the stream.""" 
     184        try: 
     185            stream = cherrypy.request.wsgi_environ.get('wsgi.errors') 
     186        except AttributeError, KeyError: 
     187            pass 
     188        else: 
     189            stream.flush() 
     190     
     191    def emit(self, record): 
     192        """Emit a record.""" 
     193        try: 
     194            stream = cherrypy.request.wsgi_environ.get('wsgi.errors') 
     195        except AttributeError, KeyError: 
     196            pass 
     197        else: 
     198            try: 
     199                msg = self.format(record) 
     200                fs = "%s\n" 
     201                import types 
     202                if not hasattr(types, "UnicodeType"): #if no unicode support... 
     203                    stream.write(fs % msg) 
     204                else: 
     205                    try: 
     206                        stream.write(fs % msg) 
     207                    except UnicodeError: 
     208                        stream.write(fs % msg.encode("UTF-8")) 
     209                self.flush() 
     210            except: 
     211                self.handleError(record) 
  • trunk/cherrypy/tutorial/tut01_helloworld.py

    r1120 r1619  
    2323 
    2424 
     25import os.path 
     26tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf') 
     27 
    2528if __name__ == '__main__': 
    26     import os.path 
    27     tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf') 
    28     cherrypy.config.update(tutconf) 
    2929    # CherryPy always starts with app.root when trying to map request URIs 
    3030    # to objects, so we need to mount a request handler root. A request 
    3131    # to '/' will be mapped to HelloWorld().index(). 
    32     cherrypy.quickstart(HelloWorld()
     32    cherrypy.quickstart(HelloWorld(), config=tutconf
    3333else: 
    3434    # This branch is for the test suite; you can ignore it. 
    35     cherrypy.tree.mount(HelloWorld()
     35    cherrypy.tree.mount(HelloWorld(), config=tutconf

Hosted by WebFaction

Log in as guest/cpguest to create tickets