`ValueError: Unable to configure handler 'mail_admins'`.
The `mail_admins` section of the configuration is the same as the
[https://docs.djangoproject.com/en/3.1/topics/logging/#django.utils.log.AdminEmailHandler
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"),
},
},
}
)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32016>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* resolution: => needsinfo
Comment:
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.).
--
Ticket URL: <https://code.djangoproject.com/ticket/32016#comment:1>
Comment (by Susana Cárdenas Molinar):
Replying to [comment:1 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.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32016#comment:2>
* Attachment "ticket_32016.tar" added.
Sample project.
Comment (by 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:
[https://github.com/django/django/pull/13446 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' 🤔
--
Ticket URL: <https://code.djangoproject.com/ticket/32016#comment:4>
* has_patch: 0 => 1
* severity: Release blocker => Normal
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/32016#comment:5>
* owner: (none) => Carlton Gibson <carlton.gibson@…>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"2a55431a5678af52f669ffe7dff3dd0bd21727f8" 2a55431a]:
{{{
#!CommitTicketReference repository=""
revision="2a55431a5678af52f669ffe7dff3dd0bd21727f8"
Fixed #32016 -- Clarified manual logging config docs.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32016#comment:6>
Comment (by Carlton Gibson <carlton.gibson@…>):
In [changeset:"2c629b37d5d2aad93223f4ea3b7fbfa324841c2b" 2c629b37]:
{{{
#!CommitTicketReference repository=""
revision="2c629b37d5d2aad93223f4ea3b7fbfa324841c2b"
[3.1.x] Fixed #32016 -- Clarified manual logging config docs.
Backport of 2a55431a5678af52f669ffe7dff3dd0bd21727f8 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32016#comment:7>
Comment (by Susana Cárdenas Molinar):
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/32016#comment:8>