#32016 closed Bug (fixed)
mail_admins configuration crashes with ImproperlyConfigured.
Reported by: | Susana Cárdenas Molinar | Owned by: | |
---|---|---|---|
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)
Change History (10)
follow-up: 2 comment:1 by , 4 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
Summary: | ValueError: Unable to configure handler 'mail_admins' on custom logging configuration → mail_admins handler cannot be configured. |
comment:2 by , 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 , 4 years ago
Cc: | added |
---|---|
Resolution: | needsinfo |
Severity: | Normal → Release blocker |
Status: | closed → new |
Summary: | mail_admins handler cannot be configured. → mail_admins configuration crashes with ImproperlyConfigured. |
Triage Stage: | Unreviewed → Accepted |
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.
follow-up: 9 comment:4 by , 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 , 4 years ago
Has patch: | set |
---|---|
Severity: | Release blocker → Normal |
Triage Stage: | Accepted → Ready for checkin |
comment:8 by , 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.
comment:9 by , 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.
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.).