抑制 Django mod_wsgi IOErrors

发布于 2025-01-08 11:09:07 字数 2108 浏览 0 评论 0原文

这些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身软味 2025-01-15 11:09:07

最近出现了一些问题,现在在 trunk/1.4beta 上将其作为 UnreadedPostError 提出。请参阅 https://code.djangoproject.com/ticket/17277https://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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文