如何记录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?
当前回答
下面是一个简单的例子,摘自python 2.6文档:
import logging
LOG_FILENAME = '/tmp/logging_example.out'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,)
logging.debug('This message should go to the log file')
其他回答
下面是一个简单的例子,摘自python 2.6文档:
import logging
LOG_FILENAME = '/tmp/logging_example.out'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,)
logging.debug('This message should go to the log file')
你可以记录主线程上所有未捕获的异常,方法是将一个处理程序分配给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.
您可以使用记录器在任何级别(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)
未捕获的异常消息将发送到STDERR,因此,您可以使用任何用于运行Python脚本的shell将STDERR发送到文件中,而不是在Python中实现日志记录。在Bash脚本中,您可以使用输出重定向来实现这一点,如Bash指南中所述。
例子
附加错误文件,其他输出到终端:
./test.py 2>> mylog.log
覆盖交错的STDOUT和STDERR输出文件:
./test.py &> mylog.log