[英]django: How can I create a custom Logging Filter for SuspiciousOperation exception?
After migrate to 1.11 ( from 1.8 ) I'm receiving some SuspiciousOperation
errors from logging. 从1.8迁移到1.11(从1.8)后,我从日志记录中收到一些SuspiciousOperation
错误。
It seems it comes from JS request who keeps session alive if user move their mouse. 似乎来自JS请求,如果用户移动鼠标,该请求可使会话保持活动状态。 But this is not important. 但这并不重要。
How can I filter just this exception? 如何仅过滤此异常?
What I tried: 我试过的
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,
},
}
}
But I'm still receiving the error: 但是我仍然收到错误:
Internal Server Error: /keepalive 内部服务器错误:/ 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
I am not sure about the correct answer, but I think django is catching the SuspiciousOperation at WSGI level and is logging an ERROR. 我不确定正确的答案,但是我认为django在WSGI级别上正在捕获SuspiciousOperation,并且正在记录错误。 See: https://docs.djangoproject.com/en/dev/ref/exceptions/#suspiciousoperation 参见: https : //docs.djangoproject.com/en/dev/ref/exceptions/#suspiciousoperation
If a SuspiciousOperation exception reaches the WSGI handler level it is logged at the Error level and results in a HttpResponseBadRequest. 如果SuspiciousOperation异常达到WSGI处理程序级别,则将其记录为错误级别,并导致HttpResponseBadRequest。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.