问题描述
从1.8迁移到1.11(从1.8)后,我从日志记录中收到一些SuspiciousOperation
错误.
After migrate to 1.11 ( from 1.8 ) I'm receiving some SuspiciousOperation
errors from logging.
似乎来自JS请求,如果用户移动鼠标,该请求可使会话保持活动状态.但这并不重要.
It seems it comes from JS request who keeps session alive if user move their mouse. But this is not important.
如何仅过滤此异常?
我尝试过的事情:
我刚刚在某处创建了一个过滤器:
I just created a filter somewhere:
import logging
from django.core.exceptions import SuspiciousOperation
class StopSuspiciousOperation(logging.Filter):
def filter(self, record):
if record.exc_info:
exc_value = record.exc_info[1]
return isinstance(exc_value, SuspiciousOperation)
return True
然后我将此过滤器添加到我的配置中:
Then I added this filter to my configuration:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse',
},
'stop_suspicious_operation': {
'()': 'aula.utils.loggingFilters.StopSuspiciousOperation',
}
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false',
'stop_suspicious_operation',], #<-- here
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
}
}
但是我仍然收到错误:
内部服务器错误:/keepalive
Internal Server Error: /keepalive
SuspiciousOperation at /keepalive
The request's session was deleted before the request completed. The user may have logged out in a concurrent request, for example.
Request Method: GET
Request URL: https://XXXXXX/YYYYYY
Django Version: 1.11.9
Python Executable: /usr/bin/python
Python Version: 2.7.3
推荐答案
我不确定正确的答案,但是我认为django在WSGI级别捕获了SuspiciousOperation,并记录了一个错误.看: https://docs.djangoproject.com/en/dev/ref/exceptions /#可疑操作
I am not sure about the correct answer, but I think django is catching the SuspiciousOperation at WSGI level and is logging an ERROR. See:https://docs.djangoproject.com/en/dev/ref/exceptions/#suspiciousoperation
您可能只想过滤出这样的错误请求:
You maybe just want to filter out the bad requests like this:
from logging import LogRecord
def filter_400(record: LogRecord) -> bool:
'''Filter out HTTP Error Code 400 Bad Request'''
return record.status_code != 400
这篇关于django:如何为SuspiciousOperation异常创建自定义日志记录过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!