How to set up a custom exception handler in Tornado?

I am trying to implement selective logging of raw requests with an override of the tornado.web.Application._handle_request_exception method:

 def _handle_request_exception(self, e): logging.error('error') # Just for test. 

But I see the same log output as:

 2012-09-01 03:35:09,947 [7399] root ERROR: Uncaught exception GET / (127.0.0.1) HTTPRequest(...) Traceback (most recent call last): 

instead of my custom post. What am I doing wrong?

+4
source share
1 answer

Well, firstly, the _handle_request_exception method is in the RequestHandler , not the Application .

Secondly, you cannot override the associated method with a new definition in the main namespace:

 def _handle_request_exception(self, e): logging.error('error') 

You need to subclass the RequestHandler class:

 class BaseHandler(tornado.web.RequestHandler): def _handle_request_exception(self, e): logging.error('error') 

all your handlers should then inherit from BaseHandler .

+6
source

All Articles