How to Set Up Django Mail for Error Reporting

by Shahid Ashraf

In Django, when we set debug = false, error pages are hidden from the web application user. In these cases, we need to trace the log files and this can be time consuming. However, Django provides a cool way to inform web application administrators about errors (usually 500 errors). This blog will outline how to enable django admin emails for error reporting. This has to be in your check-list for deploying your applications on production.

Let’s get into setting this up,

Step 1: Set up Django email

In setting define the following parameters.

MAILER_LIST = ['shahid@trialx.com']

EMAIL_HOST = 'smtp.XXXX’

EMAIL_HOST_USER = 'abc'

EMAIL_HOST_PASSWORD = ‘password’

EMAIL_PORT = 587

EMAIL_USE_TLS = True

DEFAULT_FROM_EMAIL = 'info@trialx.com'

We can simply test the above configuration by using:

$> python manage.py shell

>>from django.core.mail import send_mail

>>send_mail(subject, message, DEFAULT_FROM_EMAIL,

MAILER_LIST, fail_silently=False)

Step 2: Add your email to settings

You need to add the email you want to get the error mails from into Django application.

In settings define,

ADMINS = [(‘Shahid’, ‘shahid@trialx.com’)]

Step 3 Define handler and logger

In settings we need to configure handler and logger for error mail.

LOGGING = {

'version': 1,

'disable_existing_loggers': True,

'formatters': {

'verbose': {

'format': '%(levelname)s [%(asctime)s] %(module)s %(message)s'

},

},

'handlers': {

'console': {

'level': 'DEBUG',

'class': 'logging.StreamHandler',

'formatter': 'simple'

},

'file': {

'class': 'logging.handlers.RotatingFileHandler',

'formatter': 'verbose',

'filename': '/var/www/logs/ibiddjango.log',

'maxBytes': 1024000,

'backupCount': 3,

},

'mail_admins': {

'level': 'ERROR',

'class': 'django.utils.log.AdminEmailHandler'

}

},

'loggers': {

'django': {

'handlers': ['file', 'console',’mail_admins’],

'propagate': True,

'level': 'DEBUG',

},

}

}

This is a simple way to configure error reporting via email. For more stay connected!

Leave a Reply

Your email address will not be published. Required fields are marked *

Tools & Practices

Tools and Technologies we use at Applied

Contact us now

Popular Posts