App Engine Tracebacks via E-Mail
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.