是否可以将Django的SafeExceptionReporterFilter与AdminEmailHandler之外的其他东西一起使用?

2024-06-28 05:22:24 发布

您现在位置:Python中文网/ 问答频道 /正文

{sensitive to filter of Django's'正在尝试筛选出^敏感信息。我以为在几个特定的选项上加上这些注释就足够了,但这行不通。我在SafeExceptionReporterFilter中设置了断点,它只在从AdminEmailHandler调用时中断,而不是从其他处理程序调用。我错过了什么?在


Tags: oftodjango信息处理程序选项filter断点
2条回答

您可以编写一个自定义的Handler,它使用django.views.debug.ExceptionReporter来格式化异常。在

ExceptionReporter的用法示例:

from django.views.debug import ExceptionReporter

# exc_type, exc_value, traceback are a standard exception
# tuple as returned by sys.exc_info
reporter = ExceptionReporter(request, exc_type, exc_value, traceback)
html_report = reporter.get_traceback_html()
text_report = reporter.get_traceback_text()

ExceptionReporter将使用由DEFAULT_EXCEPTION_REPORTER_FILTER设置定义的ExceptionReporterFilter,默认情况下是SafeExceptionReporterFilter。在

查看AdminEmailHandlerimplementation,了解如何创建自定义Handler。在

即使使用SafeExceptionReporterFilter,异常仍将包含敏感数据(例如,服务器的ENV变量和其他运行时数据)。在

为避免暴露敏感数据,不应使用此筛选器。相反,编写自己的异常处理程序中间件并有选择地(递归地?)从日志中获取所需的数据。在

请参阅^{},了解如何获取异常的回溯以及如何根据需要使用它。在

即使您使用CustomHandler,也会受到特定处理程序的限制,而且据我所知,第三方处理程序不会使用SafeExceptionReporterFilter。在

相关问题 更多 >