是否可以捕获Python应用程序生成的任何回溯?(Is it possible to capture any traceback generated by a Python application?)

编程入门 行业动态 更新时间:2024-10-23 08:31:30
是否可以捕获Python应用程序生成的任何回溯?(Is it possible to capture any traceback generated by a Python application?)

我一直在搜索谷歌以某种方式捕获Python应用程序生成的任何回溯。

如果发生任何错误而产生回溯(而不是依赖用户向我报告问题),我想向自己发送电子邮件/冗余/通知。

我仍然没有发现任何不涉及你尝试/除外的事情 。 但是当然我不能把我所做的一切都放在单独的try / except子句中,因为我正在编写启动UI的应用程序(PySide / PyQt4 / PySide2 / PyQt5),并且可能会出现用户交互错误。

这是可能的,如果是这样,我如何捕获生成的任何回溯?

I've been searching google for a way to somehow capture any traceback generated by a Python application.

I'd like to send an email/slack/notification to myself if any error occurs which generates a traceback (instead of relying on users to report issues to me).

I still haven't found anything which doesn't involve you doing a try/except. But of course I can't put everything I do inside individual try/except clauses since I'm writing applications which launch a UI (PySide/PyQt4/PySide2/PyQt5) and could error on user interaction.

Is this possible, and if so how can I capture any traceback generated?

最满意答案

您可以通过创建自定义sys.excepthook轻松完成:

import sys import traceback def report_exception(exc_type, exc_value, exc_tb): # just a placeholder, you may send an e-mail here print("Type", exc_type) print("Value", exc_value) print("Tb", ''.join(traceback.format_tb(exc_tb))) def custom_excepthook(exc_type, exc_value, exc_tb): report_exception(exc_type, exc_value, exc_tb) sys.__excepthook__(exc_type, exc_value, exc_tb) # run standard exception hook sys.excepthook = custom_excepthook raise RuntimeError("I want to report exception here...")

对于漂亮打印的回溯对象,请参阅回溯模块。

You can easily do it by creating custom sys.excepthook:

import sys import traceback def report_exception(exc_type, exc_value, exc_tb): # just a placeholder, you may send an e-mail here print("Type", exc_type) print("Value", exc_value) print("Tb", ''.join(traceback.format_tb(exc_tb))) def custom_excepthook(exc_type, exc_value, exc_tb): report_exception(exc_type, exc_value, exc_tb) sys.__excepthook__(exc_type, exc_value, exc_tb) # run standard exception hook sys.excepthook = custom_excepthook raise RuntimeError("I want to report exception here...")

For pretty-printing traceback objects refer to traceback module.

更多推荐

本文发布于:2023-08-07 16:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465073.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   capture   Python   application   generated

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!