抑制 Django mod_wsgi IOErrors
这些 IOError 是众所周知的,如其他 SO 线程中所示(见下文),并且可能是由用户在上传过程中关闭浏览器引起的。有关为什么没有简单解决方案的讨论,请参阅此 django-developers线程。
我首先尝试了 this ioerror SO thread 的答案中描述的中间件选项(除了我们的案例我会根据请求 uri 进行分支)。 它不起作用:我可以捕获响应并看到就 Django 而言,帖子中没有包含文件,并且它返回了带有验证错误的 200 响应。 但是,我仍然可以在 Apache 的错误日志中看到 IOError。
接下来,我们发现 关于 django bug 的讨论 建议您使用以下命令抑制此问题日志记录过滤器。我尝试将过滤器附加到我们的自定义记录器、根记录器和“django.request”...我仍然看不到 IOError 来抑制它!我只能看到与表单验证失败相关的日志消息。
有什么建议吗?
[编辑:这是我的设置]
在settings.py中(主要是从Django的conf/global_settings.py
复制):
DEBUG = False
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'ioerrsquash': {
'()': 'es.ioerr_filter.IOErrorFilter'
} },
'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
'filters': ['ioerrsquash']
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
'wsgi.errors': { # added this logger after django.request failed
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
}
}
}
ioerr_filter.py的内容(显然只是为了测试目的):
import logging
import sys
from django.conf import settings
class IOErrorFilter(logging.Filter):
def filter(self, record):
LOGF = "%s/BU_IOERR.log" % settings.LOG_FILE_DIR
f = open(LOGF, 'a')
f.write('... record\n')
f.write(str(record))
f.write('\n... exc_info\n')
f.write(str(sys.exc_info()))
f.write('\n...\n')
return True
These IOErrors are fairly well known, as seen in other SO threads (see below), and can be caused by a user closing their browser mid-upload. For a discussion of why there is no simple solution, see this django-developers thread.
I first tried the middleware option described in an answer to this ioerror SO thread (except in our case I would branch on the request uri). It didn't work: I could capture the response and see that as far as Django was concerned, there wasn't a file included in the post, and it was returning a 200 response with the validation error. And yet, I can still see the IOError in Apache's error log.
Next we found this discussion on a django bug which suggests you should suppress this with a Logging filter. I tried attaching the filter to our custom logger, the root logger and 'django.request' ... I still can't see the IOError to suppress it! I can only see log messages associated with the failed validation of the form.
Any suggestions?
[ Edit: here is my setup ]
In settings.py (mostly copied from Django's conf/global_settings.py
):
DEBUG = False
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'ioerrsquash': {
'()': 'es.ioerr_filter.IOErrorFilter'
} },
'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
'filters': ['ioerrsquash']
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
'wsgi.errors': { # added this logger after django.request failed
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
}
}
}
Content of ioerr_filter.py (obviously just for testing purposes):
import logging
import sys
from django.conf import settings
class IOErrorFilter(logging.Filter):
def filter(self, record):
LOGF = "%s/BU_IOERR.log" % settings.LOG_FILE_DIR
f = open(LOGF, 'a')
f.write('... record\n')
f.write(str(record))
f.write('\n... exc_info\n')
f.write(str(sys.exc_info()))
f.write('\n...\n')
return True
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最近出现了一些问题,现在在 trunk/1.4beta 上将其作为
UnreadedPostError
提出。请参阅 https://code.djangoproject.com/ticket/17277 和 https://code.djangoproject.com/ticket/17069。您应该能够使用日志过滤器来抑制这些错误。 #17069 中有一个例子。There are more recent issues where this is now raised as an
UnreadablePostError
on trunk/1.4beta. See https://code.djangoproject.com/ticket/17277 and https://code.djangoproject.com/ticket/17069. You should be able to use a logging filter to suppress these errors. There is an example in #17069.