前言
线上的程序报错的时候,使用 python
的标准库 logging
记录的日志 debug
问题,是我们常用的操作,但是 logging
没有直接提供给我们打印变量值的功能,这个需要我们显性的写在日志中,就像这样: logger.debug(f'error: {a}')
但是错误是一个链条,不是一个点,如果在每处都加上打印语句的话,工作量太大了
python
的第三方日志库 loguru
,可以很好的帮助我们实现这个需求
效果展示
你想要实现下面的效果吗?
2022-01-09 15:59:52.058 | ERROR| __main__:func:32 - Expecting value: line 1 column 1 (char 0)
Traceback (most recent call last):File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 930, in _bootstrap
self._bootstrap_inner()
│└
└
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 973, in _bootstrap_inner
self.run()
│└
└
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
│││││└ {}
││││└
│││└ (, <_queue.SimpleQueue object at 0x1053e88b0>, None, ())
││└
│└
└
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/concurrent/futures/thread.py", line 77, in _worker
work_item.run()
│└
└
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/concurrent/futures/thread.py", line 52, in run
result = self.fn(*self.args, **self.kwargs)
│││││└ {}
││││└
│││└ ()
││└
│└
└ > File "/Users/bot/Desktop/code/ideaboom/test_zjwt/test_api_copy.py", line 27, in func
assert int(response.json().get('r')) == (a+b)
│││ └ 54
││└ 100
│└
└ File "/Users/bot/.local/share/virtualenvs/ideaboom-8ZWsq-JB/lib/python3.9/site-packages/requests/models.py", line 910, in json
return complexjson.loads(self.text, **kwargs)
││││└ {}
│││└ 【python 使用 loguru 输出异常日志同时打印变量值】││└
│└
└
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
││└ '\n500 Internal Server Error - 锐客网 \nInternal Server Error...
│└
└
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
││││└ '\n500 Internal Server Error - 锐客网 \nInternal Server Error...
│││└
││└ '\n500 Internal Server Error - 锐客网 \nInternal Server Error...
│└
└
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
│└ '\n500 Internal Server Error - 锐客网 \nInternal Server Error...
└ json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
文章图片
一个简单的例子 使用
loguru
吧!loguru
提供了 exception
方法来打印异常logger.exception()
需要一个参数,随便填什么都可以,我习惯用 error
效果展示:logger.exception
和logger.error
不一样。前者会做变量跟踪,但是后者不会
from loguru import loggerdef func(a: int, b: int):
a/btry:
func(0, 0)
except Exception as error:
logger.exception(error)
─?python -u "/Users/bot/Desktop/code/ideaboom/test_logger/003.py"
2022-01-09 23:44:12.792 | ERROR| __main__::11 - division by zero
Traceback (most recent call last):> File "/Users/bot/Desktop/code/ideaboom/test_logger/003.py", line 9, in
func(0, 0)
└ File "/Users/bot/Desktop/code/ideaboom/test_logger/003.py", line 5, in func
a/b
│ └ 0
└ 0ZeroDivisionError: division by zero
从图中可以看到,日志展示时候,打印了变量
a
和 b
的值文章图片
链式异常
loguru
对链式异常的支持也很好如果你不知道什么是异常链,可以看 Python 官方文档——异常链
from loguru import loggerdef func(a: int, b: int):
try:
a/b
except Exception as error:
raise Exception('计算错误') from errortry:
func(0, 0)
except Exception as error:
logger.exception(error)
─?python -u "/Users/bot/Desktop/code/ideaboom/test_logger/003.py"
2022-01-09 23:43:04.729 | ERROR| __main__::14 - 计算错误
Traceback (most recent call last):File "/Users/bot/Desktop/code/ideaboom/test_logger/003.py", line 6, in func
a/b
│ └ 0
└ 0ZeroDivisionError: division by zeroThe above exception was the direct cause of the following exception:Traceback (most recent call last):> File "/Users/bot/Desktop/code/ideaboom/test_logger/003.py", line 12, in
func(0, 0)
└ File "/Users/bot/Desktop/code/ideaboom/test_logger/003.py", line 8, in func
raise Exception('计算错误') from error
└ Exception('计算错误')Exception: 计算错误
文章图片
推荐阅读
- 推荐系统论文进阶|CTR预估 论文精读(十一)--Deep Interest Evolution Network(DIEN)
- Python专栏|数据分析的常规流程
- Python|Win10下 Python开发环境搭建(PyCharm + Anaconda) && 环境变量配置 && 常用工具安装配置
- Python绘制小红花
- Pytorch学习|sklearn-SVM 模型保存、交叉验证与网格搜索
- OpenCV|OpenCV-Python实战(18)——深度学习简介与入门示例
- python|8. 文件系统——文件的删除、移动、复制过程以及链接文件
- 爬虫|若想拿下爬虫大单,怎能不会逆向爬虫,价值过万的逆向爬虫教程限时分享
- 分布式|《Python3网络爬虫开发实战(第二版)》内容介绍
- java|微软认真聆听了开源 .NET 开发社区的炮轰( 通过CLI 支持 Hot Reload 功能)