disable django error emails

How to Disable Django Error Emails

By default, Django sends error emails to system administrators whenever there is an error on your website or application. This can be very annoying because if an error occurs on a high traffic page, you may get a lot of unnecessary emails. This may even get your email account blocked by the email service provider. So sometimes you may need to disable Django error emails. Here are the steps to disable Django error emails.


How to Disable Django Error Emails

Here are the steps to disable Django error emails. There are different ways to disable Django error emails. We will look at each of them one by one.


1. Set disable_existing_loggers to True

One of the simplest ways to disable logging is to simply set disable_existing_loggers variable to True in your Django’s LOGGING settings.

LOGGING = {
    ...
    'version': 1,                       # the dictConfig format version
    'disable_existing_loggers': True,  # retain the default loggers
    ...
}

The problem with this approach is that if you define a logging module in any of the applications or other modules, then all Django loggers are enabled back, and you will start receiving error emails as before. So use this method if you don’t intend to create your own logger in any part of your website/application.


2. Redefine Django Logger

Next option is to redefine the Django logger to override its default behavior. Here is a sample configuration for it.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        # Redefining the logger for the `django` module
        # prevents invoking the `AdminEmailHandler`
        'django': {
            'handlers': ['console'],
            'level': 'INFO',
        },
    }
}

In this case, if you have defined any child loggers or if there are any other pre-defined loggers such request logger that inherits from the default logger, then it will break when we redefine the default logger. This is because when we redefine the Django logger, we will disable Django’s mail_admins handler.


3. Copy the logger

In this case, we keep the default Django configuration but only remove mail_admins handler. So it doesn’t break anything downstream. Here is a simple example to do so by modifying the default dictionary.

from copy import deepcopy
from django.utils.log import DEFAULT_LOGGING

logging_dict = deepcopy(DEFAULT_LOGGING)
logging_dict['loggers']['django']['handlers'] = ['console']
LOGGING = logging_dict

Please note, if Django makes any future changes to DEFAULT_LOGGING dictionary, then this might break. Nevertheless, it is a good way to disable Django error emails.


4. LOGGING_CONFIG=None

The last option is to set LOGGIN_CONFIG system variable to None. It will prevent Django from logging anything.

import logging.config
LOGGING_CONFIG = None
logging.config.dictConfig({
    "version": 1,
    "disable_existing_loggers": False,
})

Thereafter, you can specifically define your own logging as to what to log and how to communicate issues to admins, depending on your requirement. Unfortunately, Django does not allow you to selectively disable error emails for specific errors. You have to disable it completely and then define your own custom logging. Again, this is also a good ways to stop Django error emails.

That’s it. In this article, we have learnt how to disable Django error mails. It is important to note that Django error emails are actually quite useful and can help you detect uncaught errors in your website/application. Our suggestion is to simply add a try-catch block around the code that triggers Django error emails, instead of disabling them completely.

Also read:

How to Add Minutes, Hours, Months to Datetime in PostgreSQL
Find Files with Special Characters in Filename in Linux
Find Last Occurrence of Character or Substring in JS
How to Split Python List into N Sublists
How to Insert Text At Certain Line in Linux

Leave a Reply

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