第一种方法使用logging模块
import logging
def test(self):
try:
1 / 0 # 触发异常
except BaseException as e:
logging.exception(e) # 方式2
finally:
pass
第二种方法使用traceback模块
import traceback
def test(self):
try:
1 / 0 # 触发异常
except BaseException as e:
msg = traceback.format_exc()
print (msg)
finally:
pass