我正在尝试创建一个基于类的日志记录处理程序,当应用程序使用 django 提供的一些内置日志记录配置看到 DisallowedHost 异常时,它会通知某些第三方服务。
但是,我遇到了一个import
我无法理解如何解决的特定错误。
我的设置.py
LOGGING = {
...
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'notify_my_service': {
'level': 'ERROR',
'class': 'tools.exception_logging.NotifyMyServiceHandler'
}
},
'loggers': {
...
'django.security.DisallowedHost': {
'handlers': ['notify_my_service'],
'propagate': False,
},
},
}
我的异常处理程序:
import logging
class NotifyMyServiceHandler(logging.handlers.HTTPHandler):
def emit(self, error):
doSomething()
大追溯
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 117, in inner_run
autoreload.raise_last_exception()
File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 250, in raise_last_exception
six.reraise(*_exception)
File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 22, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/usr/local/lib/python2.7/dist-packages/django/utils/log.py", line 75, in configure_logging
logging_config_func(logging_settings)
File "/usr/lib/python2.7/logging/config.py", line 794, in dictConfig
dictConfigClass(config).configure()
File "/usr/lib/python2.7/logging/config.py", line 576, in configure
'%r: %s' % (name, e))
ValueError: Unable to configure handler 'notify_my_service': 'module' object has no attribute 'handlers'
因此,由于某种原因,日志模块似乎没有正确导入。我已经尝试过来自其他线程的示例,例如在异常处理程序模块中导入设置。
是否可以以这种方式为 django 日志编写自定义处理程序?