Django: ValueError: unable to configure handle "loggers"
The Problem
You are trying to configure logging for your Django application, but keep getting the following error message:
ValueError: Unable to configure handler 'loggers'
This is your logging configuration dictionary in settings.py:
import os
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"root": {
"handlers": ["console"],
"level": "WARNING",
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
},
"loggers": {
"django": {
"handlers": ["console"],
"level": os.getenv("DJANGO_LOG_LEVEL", "INFO"),
"propagate": False,
},
},
},
}
What is the cause of this error and how do you fix it?
The Solution
The error is caused by a mistake in the structure of the logging dictionary. In the dictionary above, loggers is an element of handlers, but it should be a top-level element, per the Django documentation. A bare-bones LOGGING dictionary should be structured as follows:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
# add formatters here (optional)
},
'handlers': {
# add handlers here
},
'loggers': {
# add loggers here
},
}
Here is a correctly indented version of the LOGGING dictionary:
import os
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"root": {
"handlers": ["console"],
"level": "WARNING",
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
},
},
"loggers": {
"django": {
"handlers": ["console"],
"level": os.getenv("DJANGO_LOG_LEVEL", "INFO"),
"propagate": False,
},
},
}
Considered "not bad" by 4 million developers and more than 150,000 organizations worldwide, Sentry provides code-level observability to many of the world's best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.