Django Logging: ValueError: Unable to configure handler 'gunicorn'

I configured logging for a new Django project, and here is the code for my settings.py:

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "warnfile": {
            "level": "WARNING",
            "class": "logging.FileHandler",
            "filename": BASE_DIR / "warning.log",
            "maxBytes": 1024 * 1024 * 25,  # 100 mb
        },
        "errorfile": {
            "level": "ERROR",
            "class": "logging.FileHandler",
            "filename": BASE_DIR / "error.log",
            "maxBytes": 1024 * 1024 * 25,  # 100 mb
        },
        "gunicorn": {
            "level": "DEBUG",
            "class": "logging.handlers.RotatingFileHandler",
            "formatter": "verbose",
            "filename": BASE_DIR / "gunicorn.log",
            "maxBytes": 1024 * 1024 * 25,  # 100 mb
        },
    },
    "loggers": {
        "": { # keeping it empty for all kinds of loggers
            "handlers": [
                "warnfile",
                "errorfile",
            ], 
            "level": "WARNING",
            "propagate": True,
        },
        "gunicorn.errors": {
            "level": "DEBUG",
            "handlers": ["gunicorn"],
            "propagate": True,
        },
    },
}

Let me explain: I want to put 2 kinds of logs: warnings, and errors, in 2 differents files. I also want a third file for gunicorn logs to monitor the server side.

When I run the server, I get the following error:

ValueError: Unable to configure handler 'gunicorn'

I also created all the files just in case the logger does't create them for me, but still no changes.

Here is my full traceback:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\logging\config.py", line 710, in configure_handler
    formatter = self.config['formatters'][formatter]
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\logging\config.py", line 326, in __getitem__
    value = dict.__getitem__(self, key)
KeyError: 'verbose'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\logging\config.py", line 565, in configure
    handler = self.configure_handler(handlers[name])
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\logging\config.py", line 712, in configure_handler
    raise ValueError('Unable to set formatter '
ValueError: Unable to set formatter 'verbose'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "D:\Data\Dropbox\My Laptop\Tasbiq\V1\v1proj\v1env\lib\site-packages\sentry_sdk\integrations\threading.py", line 69, in run
    reraise(*_capture_exception())
  File "D:\Data\Dropbox\My Laptop\Tasbiq\V1\v1proj\v1env\lib\site-packages\sentry_sdk\_compat.py", line 56, in reraise
    raise value
  File "D:\Data\Dropbox\My Laptop\Tasbiq\V1\v1proj\v1env\lib\site-packages\sentry_sdk\integrations\threading.py", line 67, in run
    return old_run_func(self, *a, **kw)
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "D:\Data\Dropbox\My Laptop\Tasbiq\V1\v1proj\v1env\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "D:\Data\Dropbox\My Laptop\Tasbiq\V1\v1proj\v1env\lib\site-packages\django\core\management\commands\runserver.py", line 125, in inner_run
    autoreload.raise_last_exception()
  File "D:\Data\Dropbox\My Laptop\Tasbiq\V1\v1proj\v1env\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "D:\Data\Dropbox\My Laptop\Tasbiq\V1\v1proj\v1env\lib\site-packages\django\core\management\__init__.py", line 398, in execute
    autoreload.check_errors(django.setup)()
  File "D:\Data\Dropbox\My Laptop\Tasbiq\V1\v1proj\v1env\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "D:\Data\Dropbox\My Laptop\Tasbiq\V1\v1proj\v1env\lib\site-packages\django\__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "D:\Data\Dropbox\My Laptop\Tasbiq\V1\v1proj\v1env\lib\site-packages\django\utils\log.py", line 76, in configure_logging
    logging_config_func(logging_settings)
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\logging\config.py", line 811, in dictConfig
    dictConfigClass(config).configure()
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\logging\config.py", line 572, in configure
    raise ValueError('Unable to configure handler '
ValueError: Unable to configure handler 'gunicorn'

You need to define a Formatter available under the key verbose, for example like this:

"formatters": {
    "verbose": {
        "format": "%(levelname)-8s - %(message)s"
    }
}

It needs to be at the same level as handlers and loggers in your configuration.

Back to Top