django - 在 django 中间件中检索堆栈跟踪
问题描述
当 django 中间件的服务器端出现故障时,如何获取堆栈跟踪?这是我尝试过的,它只给我信息,而不是完整的堆栈。
class MonitorMiddleware(object):
def process_exception(self, request, exception):
self.error = exception.message
解决方案
从评论中回答“检索错误堆栈跟踪以保存到数据库”:
https://docs.python.org/3/library/traceback.html
使用 的方法traceback
将异常跟踪转换为字符串,然后您可以将其存储在数据库中。
同样在上面的页面上:
3.5 版中的新功能。
StackSummary 对象表示准备好格式化的调用堆栈。
带有异常对象的简短示例:
>>> an_error = IndexError('tuple index out of range')
>>> traceback.print_exception(type(an_error), an_error, an_error.__traceback__)
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
IndexError: list index out of range
另请参阅从异常对象中提取回溯信息:您不需要重新引发异常,但是回溯在__traceback__
属性中。
推荐阅读
- python - cronjob中的python脚本没有将标准输出写入文件
- r - 如何将数据框多次复制到列表中
- python - 在 docker 容器中运行时无法将 geopandas 导入 python
- powershell - 通过 ::1 端口 22 重置连接(Windows 10 + PowerShell/cmd + OpenSSH)
- python - python中的一系列值返回了错误的字符串
- javascript - 通过电子邮件作为附件发送的 HTML 可编辑表格
- .net - 授权 Azure AD b2c
- intellij-idea - 为项目存储 intelliJ 项目文件的推荐位置,其中包含的存储库/模块集将随时间变化
- javascript - 获取设置值 JavaScript 快速查看表单 - 动态
- excel - 子程序在调用时不起作用,仅在独立执行时