Search code examples
pythontornado

Error E-Mails in Tornado


I am running a project in tornado, I would prefer not to check log files regularly for uncaught errors and have "email someone" or "store in db" (preferred MongoDB).

Tornado doesn't seems to have (at least in the documentation) a method to do this. Is there a way to do this?


Solution

  • You could create as many custom exceptions handlers:

    1. The first which stores the serialized exception into Mongo (using Motor)
    2. The second, to email the serialized exception, via logging.handlers.SMTPHandler.

    Have a look to: https://docs.python.org/2/library/logging.handlers.html

    How I do, in my Tornado apps:

    • Create an ApplicationException class which stores the content of the traceback as a string
    • Save the ApplicationException instances generated on runtime by my code on exception into Cassandra or Mongo, using a save method of ApplicationException class
    • Send via email, some specific ApplicationException subclass objects using the smtp handler

    I usually use this, when implementing tornado based client / server solution, so that I can send the ApplicationException objects as json over https, to my server, which can the decode it and store in DB / email it to the admins.

    All this can be quite long to learn and implement, but it worth doing it.