[Django] #32016: ValueError: Unable to configure handler 'mail_admins' on custom logging configuration

1,304 views
Skip to first unread message

Django

unread,
Sep 17, 2020, 8:20:59 PM9/17/20
to django-...@googlegroups.com
#32016: ValueError: Unable to configure handler 'mail_admins' on custom logging
configuration
-------------------------------------+-------------------------------------
Reporter: Susana | Owner: (none)
Cárdenas Molinar |
Type: Bug | Status: new
Component: Error | Version: 3.1
reporting |
Severity: Normal | Keywords: settings, logging
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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
[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.

Django

unread,
Sep 18, 2020, 12:45:13 AM9/18/20
to django-...@googlegroups.com
#32016: mail_admins handler cannot be configured.
-------------------------------------+-------------------------------------
Reporter: Susana Cárdenas | Owner: (none)
Molinar |
Type: Bug | Status: closed
Component: Error reporting | Version: 3.1
Severity: Normal | Resolution: needsinfo

Keywords: settings, logging | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* 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>

Django

unread,
Sep 21, 2020, 7:46:56 PM9/21/20
to django-...@googlegroups.com
#32016: mail_admins handler cannot be configured.
-------------------------------------+-------------------------------------
Reporter: Susana Cárdenas | Owner: (none)
Molinar |
Type: Bug | Status: closed
Component: Error reporting | Version: 3.1
Severity: Normal | Resolution: needsinfo
Keywords: settings, logging | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Sep 22, 2020, 12:41:07 AM9/22/20
to django-...@googlegroups.com
#32016: mail_admins configuration crashes with ImproperlyConfigured.

-------------------------------------+-------------------------------------
Reporter: Susana Cárdenas | Owner: (none)
Molinar |
Type: Bug | Status: new
Component: Error reporting | Version: 3.1
Severity: Release blocker | Resolution:
Keywords: settings, logging | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* Attachment "ticket_32016.tar" added.

Sample project.

Django

unread,
Sep 22, 2020, 3:59:23 AM9/22/20
to django-...@googlegroups.com
#32016: mail_admins configuration crashes with ImproperlyConfigured.
-------------------------------------+-------------------------------------
Reporter: Susana Cárdenas | Owner: (none)
Molinar |
Type: Bug | Status: new
Component: Error reporting | Version: 3.1
Severity: Release blocker | Resolution:
Keywords: settings, logging | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Sep 22, 2020, 6:52:44 AM9/22/20
to django-...@googlegroups.com
#32016: mail_admins configuration crashes with ImproperlyConfigured.
-------------------------------------+-------------------------------------
Reporter: Susana Cárdenas | Owner: (none)
Molinar |
Type: Bug | Status: new
Component: Error reporting | Version: 3.1
Severity: Normal | Resolution:
Keywords: settings, logging | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* has_patch: 0 => 1
* severity: Release blocker => Normal
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/32016#comment:5>

Django

unread,
Sep 22, 2020, 8:16:37 AM9/22/20
to django-...@googlegroups.com
#32016: mail_admins configuration crashes with ImproperlyConfigured.
-------------------------------------+-------------------------------------
Reporter: Susana Cárdenas | Owner: Carlton
Molinar | Gibson <carlton.gibson@…>
Type: Bug | Status: closed

Component: Error reporting | Version: 3.1
Severity: Normal | Resolution: fixed

Keywords: settings, logging | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson <carlton.gibson@…>):

* 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>

Django

unread,
Sep 22, 2020, 8:17:15 AM9/22/20
to django-...@googlegroups.com
#32016: mail_admins configuration crashes with ImproperlyConfigured.
-------------------------------------+-------------------------------------
Reporter: Susana Cárdenas | Owner: Carlton
Molinar | Gibson <carlton.gibson@…>
Type: Bug | Status: closed
Component: Error reporting | Version: 3.1
Severity: Normal | Resolution: fixed
Keywords: settings, logging | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Sep 22, 2020, 2:02:18 PM9/22/20
to django-...@googlegroups.com
#32016: mail_admins configuration crashes with ImproperlyConfigured.
-------------------------------------+-------------------------------------
Reporter: Susana Cárdenas | Owner: Carlton
Molinar | Gibson <carlton.gibson@…>
Type: Bug | Status: closed
Component: Error reporting | Version: 3.1
Severity: Normal | Resolution: fixed
Keywords: settings, logging | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Reply all
Reply to author
Forward
0 new messages