Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receive django error debug report by email :

Here is my configuration in django settings :

MAILER_LIST = ['[email protected]']

EMAIL_HOST = 'toto.smtp.com'

EMAIL_HOST_USER = '[email protected]'

EMAIL_HOST_PASSWORD = 'tata'

EMAIL_PORT = 587

EMAIL_USE_TLS = True

DEFAULT_FROM_EMAIL = '[email protected]'

LOGGING = {

    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'mail_admins': {
            'level': 'DEBUG',
            'class': 'django.utils.log.AdminEmailHandler',
            'filters': [],
        }
    },
    'loggers': {
         'django.request': {
             'handlers': ['mail_admins'],
             'level': 'DEBUG',
             'propagate': True,
        },
    }

}

i've try to debug with :

from django.core.mail import EmailMessage
email = EmailMessage('Hello', 'World', to=['[email protected]'])
email.send()

And i get the test email if i put this in my settings.

i would like to receive this error report by email (it's just an example and i've added this error in my code to test the mail report) :

enter image description here

What am i missing to get the debug log by email ? The test is sending the email so it's not an email configuration problem ...

I would like to get the report by email and still show the debug page on django. And get the email event if debug is true or Not.

So i've set DEBUG = True in my settings.

Thanks and regards

like image 326
Bussiere Avatar asked Oct 22 '25 04:10

Bussiere


1 Answers

As said in another answers if you want use django build-in AdminEmailHandler, then you need provide ADMINS and MANAGERS instead of MAILER_LIST in your settings.py. Like this:

ADMINS = ['[email protected]']  # better to use another mail than EMAIL_HOST_USER
MANAGERS = ADMINS

Django's utils.log have two options for processing your DEBUG value: RequireDebugFalse and RequireDebugTrue.

So if you want send error emails to your admins (ADMINS variable in settings.py) while debug, then you may use similar settings:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue'  # log while DEBUG=True
        }
    },
    'handlers': {
        'debug_mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'filters': [require_debug_true],
        }
    },
    'loggers': {
         'django.request': {
             'handlers': ['debug_mail_admins'],
             'level': 'ERROR',
             'propagate': True,
        },
    }
}

Upd.:

Also you can use logging.handlers.SMTPHandler. Then you can write something similar to this code: https://code.djangoproject.com/ticket/15917

like image 179
Yevhenii M. Avatar answered Oct 23 '25 18:10

Yevhenii M.