loguru

@logger.catch():只会捕捉到未except的异常,并中断程序

未处理除0异常:

from loguru import  logger

@logger.catch()
def demo():
    # try:
    a = 1 / 0
    # except Exception as e:
    #     logger.error(e)
    print('11111111111111111')
if __name__ == '__main__':
    demo()

print语句并不会执行
out:

2023-01-03 16:39:35.991 | ERROR    | __main__:<module>:12 - An error has been caught in function '<module>', process 'MainProcess' (11164), thread 'MainThread' (16832):
Traceback (most recent call last):

> File "D:\soft\pythonProject\demo\demo.py", line 12, in <module>
    demo()
    └ <function demo at 0x000002C97D79F250>

  File "D:\soft\pythonProject\demo\demo.py", line 7, in demo
    a = 1 / 0

ZeroDivisionError: division by zero

except 处理除0异常后, @logger.catch() 不会生效:

from loguru import  logger

@logger.catch()
def demo():
    try:
        a = 1 / 0
    except Exception as e:
        logger.error(e)

out:

2023-01-03 16:42:39.583 | ERROR    | __main__:demo:8 - division by zero

总结:

@logger.catch() 用来打印没有预料到异常信息,方便排查问题

学习资料:
https://mp.weixin.qq.com/s?__biz=Mzg3MjU3NzU1OA==&mid=2247496180&idx=1&sn=73aaf603b1d3a2dc36623f43d2b58a13&source=41#wechat_redirect

https://www.jianshu.com/p/4cd6b209a1fe

https://blog.csdn.net/qq_31810357/article/details/124779625

posted @ 2022-12-21 20:42  chuangzhou  阅读(59)  评论(0编辑  收藏  举报