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() 用来打印没有预料到异常信息,方便排查问题
本文来自博客园,作者:chuangzhou,转载请注明原文链接:https://www.cnblogs.com/czzz/p/16997216.html