繁体   English   中英

Django 日志记录:ValueError:无法配置处理程序“gunicorn”

[英]Django Logging: ValueError: Unable to configure handler 'gunicorn'

我为一个新的 Django 项目配置了日志记录,这里是我的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,
    },
},
}

让我解释一下:我想将两种日志:警告和错误放在两个不同的文件中。 我还想要 gunicorn 日志的第三个文件来监视服务器端。

当我运行服务器时,出现以下错误:

ValueError: Unable to configure handler 'gunicorn'

我还创建了所有文件,以防记录器不为我创建它们,但仍然没有更改。

这是我的完整回溯:

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'

您需要在键verbose下定义一个可用的 Formatter,例如:

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

它需要与配置中的handlersloggers处于同一级别。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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