有一个django模板变量
TECHNICAL_500_TEMPLATE
/
TECHNICAL_500_TEXT_TEMPLATE
在
django debug view
它控制错误报告中可见的内容,当然还有错误电子邮件。注释说明模板位于python变量中,以便在模板加载程序中断时生成错误。您可以在django包中更改此变量,但我不建议这样做。
技术500_模板
由
ExceptionReporter
类在同一文件中。
班级
AdminEmailHandler
在里面
django utils log
然后使用
异常报告程序
以生成html错误报告。
您可以将
管理员邮件处理程序
并覆盖
emit
函数来包含子类版本的
异常报告程序
使用您自己定义的
技术500_模板
.
下面是一个例子:
创造
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)
然后只需设置您的django设置以在
logging section
.
LOGGING = {
# Your other logging settings
# ...
'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'project.reporter.CustomAdminEmailHandler',
'filters': ['special']
}
},
}
如果您只想隐藏设置,可以在
'settings': get_safe_settings(),
第294行,如果覆盖并复制粘贴
def get_traceback_data(self):
在您的
CustomExceptionReporter