Python重试包韧性:如何记录异常的根本原因?

2024-03-29 08:15:55 发布

您现在位置:Python中文网/ 问答频道 /正文

如本文所述,question我使用tenacity进行重试。在

玩具代码如下所示

import logging
from tenacity import retry
import tenacity


@retry(wait=tenacity.wait_incrementing(start=10, increment=10, max=100), stop=tenacity.stop_after_attempt(3))
def print_msg():
    logging.info('Hello')
    logging.info("World")
    raise Exception('Test error')


if __name__ == '__main__':
    logging.basicConfig(
        format='%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
        datefmt='%d-%m-%Y:%H:%M:%S',
        level=logging.INFO)
    logging.info('Starting')
    print_msg()

输出如下所示

^{pr2}$

有人能告诉我如何记录异常的根本原因吗?在


Tags: 代码fromimportinfologgingmsgstartstop