python print stack information method

The first method uses the logging module

import logging


def test(self):
    try:
        1 / 0  # 触发异常  
    except BaseException as e:
        logging.exception(e)  # 方式2  
    finally:
        pass

The second method uses the traceback module

import traceback


def test(self):
    try:
        1 / 0  # 触发异常  
    except BaseException as e:
        msg = traceback.format_exc()
        print (msg)
    finally:
        pass

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324777999&siteId=291194637