Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#32016 closed Bug (fixed)

mail_admins configuration crashes with ImproperlyConfigured.

Reported by: Susana Cárdenas Molinar Owned by: Carlton Gibson <carlton.gibson@…>
Component: Error reporting Version: 3.1
Severity: Normal Keywords: settings, logging
Cc: Carlton Gibson Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When upgrading to Django 3.1, the following error appears when trying to start the application on Docker:

ValueError: Unable to configure handler 'mail_admins'.

The mail_admins section of the configuration is the same as the examples shown in the documentation. This same code is working fine in Django 3.0 and when the mail_admins key is commented or deleted from the configuration, everything works properly.

This is some of the code contained in the base settings file:

DJANGO_EMAIL_ADMINS_BACKEND = os.environ.get(
    "DJANGO_EMAIL_ADMINS_BACKEND", "django.core.mail.backends.console.EmailBackend"
)
LOGGING_CONFIG = None

logging.config.dictConfig(
    {
        "version": 1,
        "disable_existing_loggers": False,
        "filters": {
            "require_debug_false": {"()": "django.utils.log.RequireDebugFalse"},
            "require_debug_true": {"()": "django.utils.log.RequireDebugTrue"},
        },
        "formatters": {
            "django.server": {
                "()": "django.utils.log.ServerFormatter",
                "format": "[%(server_time)s] %(message)s",
            }
        },
        "handlers": {
            "nodebug_console": {
                "level": "WARNING",
                "filters": ["require_debug_false"],
                "class": "logging.StreamHandler",
            },
            "debug_console": {
                "level": "INFO",
                "filters": ["require_debug_true"],
                "class": "logging.StreamHandler",
            },
            "django.server": {
                "level": "INFO",
                "class": "logging.StreamHandler",
                "formatter": "django.server",
            },
            "mail_admins": {
                "level": "ERROR",
                "filters": ["require_debug_false"],
                "class": "django.utils.log.AdminEmailHandler",
                "email_backend": DJANGO_EMAIL_ADMINS_BACKEND,
            },
        },
        "loggers": {
            "django": {
                "handlers": ["nodebug_console", "debug_console", "mail_admins"],
                "level": os.environ.get("DJANGO_LOG_LEVEL", "INFO"),
            },
            "django.server": {
                "handlers": ["django.server"],
                "level": os.environ.get("DJANGO_LOG_LEVEL", "INFO"),
                "propagate": False,
            },
            "Wikilink": {
                "handlers": ["nodebug_console", "debug_console", "mail_admins"],
                "level": os.environ.get("DJANGO_LOG_LEVEL", "INFO"),
            },
        },
    }
)

Attachments (1)

ticket_32016.tar (20.0 KB ) - added by Mariusz Felisiak 4 years ago.
Sample project.

Download all attachments as: .zip

Change History (10)

comment:1 by Mariusz Felisiak, 4 years ago

Resolution: needsinfo
Status: newclosed
Summary: ValueError: Unable to configure handler 'mail_admins' on custom logging configurationmail_admins handler cannot be configured.

Thanks for this ticket, however this configuration works for me with Django 3.1. Can you provide a small project that reproduces the issue? (without unrelated handlers, loggers, etc.).

in reply to:  1 comment:2 by Susana Cárdenas Molinar, 4 years ago

Replying to felixxm:

Thanks for this ticket, however this configuration works for me with Django 3.1. Can you provide a small project that reproduces the issue? (without unrelated handlers, loggers, etc.).

It looks like the error is not the mail_admins rather that the SECRET_KEY is improperly configured. This was working before in Django 2.2 and 3.0. Do I need to make any changes to the settings file?

This is the error I am getting before the mail_admins error:

raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.") django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

comment:3 by Mariusz Felisiak, 4 years ago

Cc: Carlton Gibson added
Resolution: needsinfo
Severity: NormalRelease blocker
Status: closednew
Summary: mail_admins handler cannot be configured.mail_admins configuration crashes with ImproperlyConfigured.
Triage Stage: UnreviewedAccepted

Thanks for details, I can reproduce this issue. It's related with setting SECRET_KEY after logging initialization, so a quick workaround is to move SECRET_KEY above the logging.config.dictConfig(...) in your settings file.

Regression in 13e4abf83e5129b44c771b2204809792087abda4, fixed on master by 948a874425e7d999950a8fa3b6598d9e34a4b861.

by Mariusz Felisiak, 4 years ago

Attachment: ticket_32016.tar added

Sample project.

comment:4 by Carlton Gibson, 4 years ago

Grrr. I think this is a documentation issue. If you configure the logging in settings then any depend on *MUST* be defined before you get there (because you brought forward the logging config call)

Here's a PR adding a suggested clarification: PR

The change in 3.1 made MainAdminHandler depend on the settings but I can't see it as a bug we should fix per se, since manually configuring settings, you're kind of on your own.

I'm not sure I see why you'd do this:

LOGGING_CONFIG = None

logging.config.dictConfig(...)

By default LOGGING_CONFIG just is 'logging.config.dictConfig' 🤔

comment:5 by Mariusz Felisiak, 4 years ago

Has patch: set
Severity: Release blockerNormal
Triage Stage: AcceptedReady for checkin

comment:6 by Carlton Gibson <carlton.gibson@…>, 4 years ago

Owner: set to Carlton Gibson <carlton.gibson@…>
Resolution: fixed
Status: newclosed

In 2a55431a:

Fixed #32016 -- Clarified manual logging config docs.

comment:7 by Carlton Gibson <carlton.gibson@…>, 4 years ago

In 2c629b37:

[3.1.x] Fixed #32016 -- Clarified manual logging config docs.

Backport of 2a55431a5678af52f669ffe7dff3dd0bd21727f8 from master

comment:8 by Susana Cárdenas Molinar, 4 years ago

Thank you for the help! I solved this by creating a separate logging file and importing it after importing all of my base settings, in case anyone else runs into this error when upgrading to Django 3.1.

in reply to:  4 comment:9 by Šarūnas Saulis, 4 years ago

Replying to Carlton Gibson:

Grrr. I think this is a documentation issue. If you configure the logging in settings then any depend on *MUST* be defined before you get there (because you brought forward the logging config call)

Here's a PR adding a suggested clarification: PR

The change in 3.1 made MainAdminHandler depend on the settings but I can't see it as a bug we should fix per se, since manually configuring settings, you're kind of on your own.

I'm not sure I see why you'd do this:

LOGGING_CONFIG = None

logging.config.dictConfig(...)

By default LOGGING_CONFIG just is 'logging.config.dictConfig' 🤔

We do this to prevent Django from loading it's default logging dict. As your own logging config gets more complex, it becomes easier to configure it from scratch, and currently Django doesn't provide an alternative - ideally it would have a setting to prevent loading of default logging dict.

In case somebody else runs into this problem, it can be worked around by preventing AdminEmailHandler from loading settings.
It just needs reported_class passed in to constructor, and can be done like this using logging config dict:

"mail_admins": {
    "level": "ERROR",
    "class": "django.utils.log.AdminEmailHandler",
    "reporter_class": "django.views.debug.ExceptionReporter",                                              
},

Should be simpler option than having to refactor how and when logging is configured.

Note: See TracTickets for help on using tickets.
Back to Top