打印堆栈信息,方便调试

方式一:

  1. import traceback
  2. def test(self):
  3. try:
  4. 1 / 0
  5. except BaseException as e:
  6. msg = traceback.format_exc()
  7. print(msg)
  8. finally:
  9. pass

方式二:

  1. import logging
  2. def test(self):
  3. try:
  4. 1 / 0
  5. except BaseException as e:
  6. # msg = traceback.format_exc()
  7. msg = logging.exception(e)
  8. print(msg)
  9. finally:
  10. pass