python 打印堆栈信息方法

第一种方法使用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
posted @ 2018-04-24 17:51  程序员同行者  阅读(3806)  评论(0编辑  收藏  举报