Django错误电子邮件太长。如何截断它?

Ale*_*exH 4 django django-errors

似乎Django 1.9中的错误电子邮件比以前更长。我认为“设置”有一个完整的部分,我认为这是多余的,而且可能也很容易透露。

编辑Django发送的错误电子邮件的最佳方法是什么?

编辑:我不只是试图隐藏敏感信息。Django 1.9中的电子邮件中包含更多内容,我想将电子邮件的格式更改为更短。我喜欢旧的方式。

Air*_*ith 5

django调试视图中有一个django模板变量TECHNICAL_500_TEMPLATE/ TECHNICAL_500_TEXT_TEMPLATE,用于控制错误报告以及错误电子邮件中的可见内容。注释说明模板在python变量中,因此,如果模板加载器损坏,可能会生成错误。您可以在django包中更改此变量,但我不建议这样做。由类在同一文件中引用。TECHNICAL_500_TEMPLATEExceptionReporter

然后AdminEmailHandlerdjango utils日志中的类使用ExceptionReporter生成html错误报告。

您可以继承AdminEmailHandler并覆盖该emit函数,以包括ExceptionReporter使用您自己定义的的子类版本TECHNICAL_500_TEMPLATE

这是一个例子:

创建reporter.py

from copy import copy

from django.views import debug
from django.utils import log
from django.conf import settings
from django import template

TECHNICAL_500_TEMPLATE = """
    # custom template here, copy the original and make adjustments
"""
TECHNICAL_500_TEXT_TEMPLATE = """
    # custom template here, copy the original and make adjustments
"""

class CustomExceptionReporter(debug.ExceptionReporter):
    def get_traceback_html(self):
        t = debug.DEBUG_ENGINE.from_string(TECHNICAL_500_TEMPLATE)
        c = template.Context(self.get_traceback_data(), use_l10n=False)
        return t.render(c)

    def get_traceback_text(self):
        t = debug.DEBUG_ENGINE.from_string(TECHNICAL_500_TEXT_TEMPLATE)
        c = template.Context(self.get_traceback_data(), autoescape=False, use_l10n=False)
        return t.render(c)

class CustomAdminEmailHandler(log.AdminEmailHandler):
    def emit(self, record):
        try:
            request = record.request
            subject = '%s (%s IP): %s' % (
                record.levelname,
                ('internal' if request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS
                 else 'EXTERNAL'),
                record.getMessage()
            )
        except Exception:
            subject = '%s: %s' % (
                record.levelname,
                record.getMessage()
            )
            request = None
        subject = self.format_subject(subject)

        no_exc_record = copy(record)
        no_exc_record.exc_info = None
        no_exc_record.exc_text = None

        if record.exc_info:
            exc_info = record.exc_info
        else:
            exc_info = (None, record.getMessage(), None)

        reporter = CustomExceptionReporter(request, is_email=True, *exc_info)
        message = "%s\n\n%s" % (self.format(no_exc_record), reporter.get_traceback_text())
        html_message = reporter.get_traceback_html() if self.include_html else None
        self.send_mail(subject, message, fail_silently=True, html_message=html_message)
Run Code Online (Sandbox Code Playgroud)

然后只需在日志记录部分中设置django设置以使用新的处理程序即可。

LOGGING = {
    # Your other logging settings
    # ...
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'project.reporter.CustomAdminEmailHandler',
            'filters': ['special']
        }
    },
}
Run Code Online (Sandbox Code Playgroud)

如果您只想隐藏设置,则可以注释掉第'settings': get_safe_settings(),294行,如果您覆盖并复制粘贴def get_traceback_data(self):CustomExceptionReporter