如何记录Python异常?
try:
do_something()
except:
# How can I log my exception here, complete with its traceback?
如何记录Python异常?
try:
do_something()
except:
# How can I log my exception here, complete with its traceback?
当前回答
也许没有那么时尚,但更简单:
#!/bin/bash
log="/var/log/yourlog"
/path/to/your/script.py 2>&1 | (while read; do echo "$REPLY" >> $log; done)
其他回答
你可以记录主线程上所有未捕获的异常,方法是将一个处理程序分配给sys. exe。excepthook,可能使用了Python日志函数的exc_info参数:
import sys
import logging
logging.basicConfig(filename='/tmp/foobar.log')
def exception_hook(exc_type, exc_value, exc_traceback):
logging.error(
"Uncaught exception",
exc_info=(exc_type, exc_value, exc_traceback)
)
sys.excepthook = exception_hook
raise Exception('Boom')
If your program uses threads, however, then note that threads created using threading.Thread will not trigger sys.excepthook when an uncaught exception occurs inside them, as noted in Issue 1230540 on Python's issue tracker. Some hacks have been suggested there to work around this limitation, like monkey-patching Thread.__init__ to overwrite self.run with an alternative run method that wraps the original in a try block and calls sys.excepthook from inside the except block. Alternatively, you could just manually wrap the entry point for each of your threads in try/except yourself.
我要找的是:
import sys
import traceback
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback_in_var = traceback.format_tb(exc_traceback)
看到的:
https://docs.python.org/3/library/traceback.html
下面是一个使用sys.excepthook的版本
import traceback
import sys
logger = logging.getLogger()
def handle_excepthook(type, message, stack):
logger.error(f'An unhandled exception occured: {message}. Traceback: {traceback.format_tb(stack)}')
sys.excepthook = handle_excepthook
您可以使用记录器在任何级别(DEBUG, INFO,…)获得跟踪。注意使用日志记录。异常时,级别为ERROR。
# test_app.py
import sys
import logging
logging.basicConfig(level="DEBUG")
def do_something():
raise ValueError(":(")
try:
do_something()
except Exception:
logging.debug("Something went wrong", exc_info=sys.exc_info())
DEBUG:root:Something went wrong
Traceback (most recent call last):
File "test_app.py", line 10, in <module>
do_something()
File "test_app.py", line 7, in do_something
raise ValueError(":(")
ValueError: :(
编辑:
这也可以(使用python 3.6)
logging.debug("Something went wrong", exc_info=True)
使用exc_info选项可能更好,保留警告或错误标题:
try:
# coode in here
except Exception as e:
logging.error(e, exc_info=True)