Django error email is too long. How do I truncate it?

只愿长相守 提交于 2019-12-06 05:43:41

问题


It seems like the error emails in Django 1.9 are much longer than they were previously. There's a whole section for "settings" which I think is superfluous and potentially too revealing.

What is the best way to edit the error email that Django sends?

edit: I am not just trying to hide sensitive information. There is a lot more content in the email in Django 1.9 and I want to change the format of the email to be shorter. I liked it the old way.


回答1:


There's a django template variable TECHNICAL_500_TEMPLATE/TECHNICAL_500_TEXT_TEMPLATE in the django debug view that controls what's visible in the error reporting, and of course error emails. A comment explains that the template is in a python variable so that errors can be generated in case the template loader breaks. You could change this variable in your django package but I don't recommend that. TECHNICAL_500_TEMPLATE is referenced by the ExceptionReporter class in the same file.

The class AdminEmailHandler in django utils log then uses the ExceptionReporter to generate the html error report.

You can subclass AdminEmailHandler and override the emit function to include your subclassed version of ExceptionReporter that uses your own defined TECHNICAL_500_TEMPLATE.

Here's an example:

Create reporter.py with

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)

And then just set your django settings to use your new handler in the logging section.

LOGGING = {
    # Your other logging settings
    # ...
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'project.reporter.CustomAdminEmailHandler',
            'filters': ['special']
        }
    },
}

If you just want to hide settings you can comment out the 'settings': get_safe_settings(), line 294, if you override and copy paste def get_traceback_data(self): in your CustomExceptionReporter



来源:https://stackoverflow.com/questions/36165790/django-error-email-is-too-long-how-do-i-truncate-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!