Skip to content

App Engine Tracebacks via E-Mail

April 30, 2009

Edit 2009-09-04: Starting with App Engine SDK 1.2.5 released today there’s now a much smarter approach. See the docstring of the ereporter module for details!

Django’s error reporting via e-mail is something I’ve missed for my App Engine applications. If you’ve followed the instructions on how to use Django on App Engine errors are already logged. But honestly, I don’t check the logs very often and sometimes it’s just a silly typo that’s fixed in a minute ;-)

So if you’ve followed the instructions you can simply extend the log_exception function to send an email after the exception is logged:

def log_exception(*args, **kwds):
  """Django signal handler to log an exception."""
  excinfo = sys.exc_info()
  cls, err = excinfo[:2]
  subject = 'Exception in request: %s: %s' % (cls.__name__, err)
  logging.exception(subject)

  # Send an email to the admins
  if 'request' in kwds:
    try:
      repr_request = repr(kwds['request'])
    except:
      repr_request = 'Request repr() not available.'
  else:
    repr_request = 'Request not available.'
  msg = ('Application: %s\nVersion: %s\n\n%s\n\n%s'
         % (os.getenv('APPLICATION_ID'), os.getenv('CURRENT_VERSION_ID'),
            ''.join(traceback.format_exception(*excinfo)),
            repr_request))
  subject = '[%s] %s' % (os.getenv('APPLICATION_ID'), subject)
  mail.send_mail_to_admins('APP_ADMIN@EXAMPLE.COM',
                           subject,
                           msg)

Make sure to replace “APP_ADMIN@EXAMPLE.COM” with the email address of an admin for this application and to add these two imports somewhere at the top of your main.py:

import traceback
from google.appengine.api import mail

You don’t need to comment this when developing your app to avoid lots of useless mails. The dev server doesn’t fully implement send_mail_to_admins() most likely as there’s no real concept of an designated admin user in the SDK. It just writes a short message to the log, even if the sendmail option is enabled.

P.s.: If you use webapp instead of Django have a look at the seventh slide of this presentation by Ken Ashcraft. He shows a similar implementation for webapp based applications.

7 Comments
  1. April 30, 2009 8:49 pm

    FYI, with DEBUG=False Django already sends exceptions via email to your ADMINS:

    http://docs.djangoproject.com/en/dev/ref/settings/#admins

  2. May 1, 2009 6:05 am

    Hi Waldemar,

    I should have mentioned that error reporting via mail works the standard Django way you’ve suggested when using the Django helper or your App Engine patch.

    The example in my post targets a “plain” Django installation on App Engine without one of those helpers where Django’s mail API is not patched to use Google’s mail API.

    I’ve never tried it with Django helper or your App Engine patch. But I assume to make admin/error mails work you’ll have to populate ADMINS manually with the administrator’s email addresses and you’ll have to set SERVER_EMAIL to an administrator’s address, right?

    Thanks for pointing at this!

    Andi

  3. May 1, 2009 3:03 pm

    Hey Andi,

    bau doch mal das Plugin in deine Posts ein: http://www.addtoany.com/

    Dann habs ichs leichter, deine Posts zu diggen :)

    • May 1, 2009 7:24 pm

      hm… ich glaube, bei wordpress.com geht das gar nicht – jedenfalls hab ich noch keinen Knopf dafür gefunden ;-)

  4. May 1, 2009 3:43 pm

    Hi Andi, yes exactly that’s all you need to do.

    Is there any reason why you don’t use one of the helper (and app-engine-patch in particular ;)?

    Liebe Grüße aus Göttingen,
    Waldemar Kornewald

    • May 1, 2009 7:30 pm

      I only use a small sub-set of Django, namely the template system, some filters and the URL mapping. So there’s not much need to add an dependency to be able to use other Django stuff.
      But I like the idea of both helpers and if I would write mode advanced application I would definitely consider to use one of them to make live easier (and to have Django’s tests!).

Trackbacks

  1. Elias Torres » Blog Archive » DeadlineExceededError, really? That wasn’t me

Comments are closed.

%d bloggers like this: