Monday, September 2, 2013

Django logging

Django has a pretty configurable logging facility.

The other day I had to customize error emails in django.

I did this by copying django's AdminEmailHandler, to a local file, and then pointing the logging utility to this.

The django guide has a large configuration that you can strip. Let's strip it down to this:

LOGGING = {
    'version': 1,
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': False,
        },
    },
}
 
Note the default configuration for the class. This is also where you copied the AdminEmailHandler. Point it to the new location that you saved it. I wasn't sure of a good place so I saved it in 'project/middleware/custom_email.py' and referenced it: "project.middleware.custom_email.AdminEmailHandler"

Now all you need to do is change the email handler to return emails the way you want. I stripped out the file so that it was just the classs and the required dependancies, because I didn't want to override other things that I didn't need to.

No comments:

Post a Comment