简体   繁体   中英

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,
# The version number of our log
"disable_existing_loggers": False,
# django uses some of its own loggers for internal operations. In case you want to disable them just replace the False above with true.
# A handler for WARNING. It is basically writing the WARNING messages into a file called WARNING.log
"formatters": {
    "main_formatter": {
        "format": "{asctime}-{levelname}-{module}-{funcName}-{message}",
        "style": "{",
    },
},
"handlers": {
    "warnfile": {
        "level": "WARNING",
        "class": "logging.FileHandler",
        "filename": BASE_DIR / "warning.log",
    },
    "errorfile": {
        "level": "ERROR",
        "class": "logging.FileHandler",
        "filename": BASE_DIR / "error.log",
    },
    "gunicorn": {
        "level": "DEBUG",
        "class": "logging.handlers.RotatingFileHandler",
        "formatter": "verbose",
        "filename": BASE_DIR / "gunicorn.log",
        "maxBytes": 1024 * 1024 * 25,  # 100 mb
    },
},
# A logger for WARNING which has a handler called 'file'. A logger can have multiple handler
"loggers": {
    # notice the blank '', Usually you would put built in loggers like django or root here based on your needs
    "": {
        "handlers": [
            "warnfile",
            "errorfile",
        ],  # notice how file variable is called in handler which has been defined above
        "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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM