<分区>
我运行一个 headless 和无人值守的服务器,它将所有内容记录到远程服务器。我在那里运行的应用程序也通过 SysLogHandler
执行此操作。它工作正常。
我遇到过程序崩溃的情况(在 try: except:
未处理的地方),我也想以这种方式记录回溯。这有可能吗?
我专门谈论意想不到的回溯,我知道我可以log exceptions in an except:
clause .
<分区>
我运行一个 headless 和无人值守的服务器,它将所有内容记录到远程服务器。我在那里运行的应用程序也通过 SysLogHandler
执行此操作。它工作正常。
我遇到过程序崩溃的情况(在 try: except:
未处理的地方),我也想以这种方式记录回溯。这有可能吗?
我专门谈论意想不到的回溯,我知道我可以log exceptions in an except:
clause .
最佳答案
您可以自定义 excepthook
.
When an exception is raised and uncaught, the interpreter calls sys.excepthook with three arguments, the exception class, exception instance, and a traceback object
import sys
import logging
logger = ...
def excepthook(type_, value, traceback):
logger.exception(value)
# call the default excepthook
sys.__excepthook__(type_, value, traceback)
sys.excepthook = excepthook
关于python - 如何记录未捕获的崩溃的回溯?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28396184/