Django错误电子邮件太长。如何截断它? [英] Django error email is too long. How do I truncate it?
问题描述
编辑Django发送的错误电子邮件的最好方法是什么?
编辑:我不只是隐藏敏感信息。 Django 1.9的电子邮件中还有更多的内容,我想将电子邮件的格式更改为更短。我喜欢旧的方式。
有一个django模板变量 TECHNICAL_500_TEMPLATE
/ TECHNICAL_500_TEXT_TEMPLATE
在 django调试视图,用于控制错误报告中显示的内容,以及错误电子邮件。一个注释解释说,模板是在一个python变量中,以便在模板加载程序中断时可以生成错误。您可以在django包中更改此变量,但不建议。 TECHNICAL_500_TEMPLATE
由同一文件中的 ExceptionReporter
类引用。
AdminEmailHandler rel =nofollow> django utils log 然后使用 ExceptionReporter
生成html错误报告。
您可以将 AdminEmailHandler
子类化,并覆盖 emit
函数以包含您的子类版本的 ExceptionReporter
使用您自己定义的 TECHNICAL_500_TEMPLATE
。
以下是一个示例:
创建 reporter.py
与
<$从django.utils导入日志
从django.conf导入设置
从django导入模板
从django.views导入调试
从django.utils导入日志
从django.conf导入设置
从django导入模板
TECHNICAL_500_TEMPLATE =
#自定义模板在这里,复制原始和make adj ü
TECHNICAL_500_TEXT_TEMPLATE =
#自定义模板在这里,复制原始并进行调整
class CustomExceptionReporter(
$ bc = template.Context(self.get_traceback_data(),use_l10n = False)$ b $($)
$ bt = debug.DEBUG_ENGINE.from_string(TECHNICAL_500_TEMPLATE)
c = b 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):
尝试:
request = record.request
subject ='%s(%s IP):%s'%(
record.levelname,
('internal'if request .META.get('REMOTE_ADDR')i n settings.INTERNAL_IPS
else'EXTERNAL'),
record.getMessage()
)
除了异常:
subject ='%s:%s'%
record.levelname
record.getMessage()
)
请求=无
subject = self.format_subject(subject)
no_exc_record = copy(record)
no_exc_record.exc_info =无
no_exc_record.exc_text =无
如果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\\\
%s%(self.format(no_exc_record),reporter.get_traceback_text())
html_message = reporter.get_traceback_html()if self.include_html else无
self.send_mail(subj ect,message,fail_silently = True,html_message = html_message)
然后只需设置您的django设置即可您在记录部分中的新处理程序。
LOGGING = {
#您的其他日志记录设置
#...
'处理程序':{
' mail_admins':{
'level':'ERROR',
'class':'project.reporter.CustomAdminEmailHandler',
'filters':['special']
}
},
}
如果您只想隐藏设置,您可以发表评论在$ code>'设置':get_safe_settings(),行294,如果您覆盖并复制粘贴 def get_traceback_data(self):
在您的 CustomExceptionReporter
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.
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
这篇关于Django错误电子邮件太长。如何截断它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!