Logging to AdminEmailHandler not working

Hi,

I have email correctly configured in my application (using SendGrid) – it’s been working for a while to send email confirmations and so forth. I just changed my logging setup to add email notification to admins so it’s like this:

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "console": {
            "level": "ERROR",
            "class": "logging.StreamHandler",
        },
        "file": {
            "level": "WARNING",
            "class": "logging.FileHandler",
            "filename": BASE_DIR / 'debug.log',
        },
        "mail_admins": {
            "level": "ERROR",
            "class": "django.utils.log.AdminEmailHandler",
        },
    },
    "loggers": {
        "django": {
            "handlers": ["file", "console", "mail_admins"],
            "propagate": True,
        },
    },
}

I restarted the application server and forced a 500 error just now. The error was logged to the file, as usual, but I didn’t receive any email (I’m the only admin, and I checked the spam folder). I tried adding another logger to make it more like the example in the documentation, but that didn’t work either. That configuration is like this:

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "console": {
            "level": "ERROR",
            "class": "logging.StreamHandler",
        },
        "file": {
            "level": "WARNING",
            "class": "logging.FileHandler",
            "filename": BASE_DIR / 'debug.log',
        },
        "mail_admins": {
            "level": "ERROR",
            "class": "django.utils.log.AdminEmailHandler",
        },
    },
    "loggers": {
        "django": {
            "handlers": ["file", "console"],
            "propagate": True,
        },
        "django.request": {
            "handlers": ["mail_admins"],
            "propagate": True,
        },
    },
}

I’m probably missing something obvious. I’ll have a look in the source code, but I appreciate any suggestions on this!

Best,
Dow

I tried replacing the logging definition with this:

from django.utils import log
LOGGING = log.DEFAULT_LOGGING

This default logging configuration should send admins emails for errors, but I’m still not getting the emails. That seems to indicate that the issue is most likely some kind of disconnect with sending the email.

I figured out one thing I missed – I didn’t have ADMINS defined in my settings. I added that:

ADMINS = [('Dow', '<my email address>'), ]

It still didn’t work. But then I saw that email_admins uses the SERVER_EMAIL setting, not the DEFAULT_FROM_EMAIL like I had assumed. The default SERVER_EMAIL is ‘root@localhost’, but that’s not an allowed sender with SendGrid. I added the setting:

SERVER_EMAIL = DEFAULT_FROM_EMAIL

and email notification of errors is now working! :grinning:

1 Like