欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python 使用 loguru 输出异常日志同时打印变量值

程序员文章站 2022-04-28 22:40:49
...

前言
线上的程序报错的时候,使用 python 的标准库 logging 记录的日志 debug 问题,是我们常用的操作,但是 logging 没有直接提供给我们打印变量值的功能,这个需要我们显性的写在日志中,就像这样: logger.debug(f’error: {a}’)

但是错误是一个链条,不是一个点,如果在每处都加上打印语句的话,工作量太大了

python 的第三方日志库 loguru ,可以很好的帮助我们实现这个需求

效果展示
你想要实现下面的效果吗?

  1. 2022-01-09 15:59:52.058 | ERROR | __main__:func:32 - Expecting value: line 1 column 1 (char 0)
  2. Traceback (most recent call last):
  3. File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 930, in _bootstrap
  4. self._bootstrap_inner()
  5. <function Thread._bootstrap_inner at 0x10556dc10>
  6. <Thread(ThreadPoolExecutor-0_14, started 6343241728)>
  7. File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 973, in _bootstrap_inner
  8. self.run()
  9. <function Thread.run at 0x10556d940>
  10. <Thread(ThreadPoolExecutor-0_14, started 6343241728)>
  11. File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 910, in run
  12. self._target(*self._args, **self._kwargs)
  13. {}
  14. <Thread(ThreadPoolExecutor-0_14, started 6343241728)>
  15. (<weakref at 0x106459450; to 'ThreadPoolExecutor' at 0x105fafd30>, <_queue.SimpleQueue object at 0x1053e88b0>, None, ())
  16. <Thread(ThreadPoolExecutor-0_14, started 6343241728)>
  17. <function _worker at 0x105fd6d30>
  18. <Thread(ThreadPoolExecutor-0_14, started 6343241728)>
  19. File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/concurrent/futures/thread.py", line 77, in _worker
  20. work_item.run()
  21. <function _WorkItem.run at 0x105fd6e50>
  22. <concurrent.futures.thread._WorkItem object at 0x106fbb4c0>
  23. File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/concurrent/futures/thread.py", line 52, in run
  24. result = self.fn(*self.args, **self.kwargs)
  25. {}
  26. <concurrent.futures.thread._WorkItem object at 0x106fbb4c0>
  27. ()
  28. <concurrent.futures.thread._WorkItem object at 0x106fbb4c0>
  29. <function func at 0x104e58040>
  30. <concurrent.futures.thread._WorkItem object at 0x106fbb4c0>
  31. > File "/Users/bot/Desktop/code/ideaboom/test_zjwt/test_api_copy.py", line 27, in func
  32. assert int(response.json().get('r')) == (a+b)
  33. 54
  34. 100
  35. <function Response.json at 0x105fb40d0>
  36. <Response [500]>
  37. File "/Users/bot/.local/share/virtualenvs/ideaboom-8ZWsq-JB/lib/python3.9/site-packages/requests/models.py", line 910, in json
  38. return complexjson.loads(self.text, **kwargs)
  39. {}
  40. <property object at 0x105fb24f0>
  41. <Response [500]>
  42. <function loads at 0x105c58a60>
  43. <module 'json' from '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py'>
  44. File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 346, in loads
  45. return _default_decoder.decode(s)
  46. '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n<title>500 Internal Server Error</title>\n<h1>Internal Server Error...
  47. <function JSONDecoder.decode at 0x105c583a0>
  48. <json.decoder.JSONDecoder object at 0x105c50130>
  49. File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 337, in decode
  50. obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  51. '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n<title>500 Internal Server Error</title>\n<h1>Internal Server Error...
  52. <built-in method match of re.Pattern object at 0x105c493f0>
  53. '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n<title>500 Internal Server Error</title>\n<h1>Internal Server Error...
  54. <function JSONDecoder.raw_decode at 0x105c58430>
  55. <json.decoder.JSONDecoder object at 0x105c50130>
  56. File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode
  57. raise JSONDecodeError("Expecting value", s, err.value) from None
  58. '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n<title>500 Internal Server Error</title>\n<h1>Internal Server Error...
  59. <class 'json.decoder.JSONDecodeError'>
  60. json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

python 使用 loguru 输出异常日志同时打印变量值
一个简单的例子
使用 loguru 吧!

loguru 提供了 exception 方法来打印异常
logger.exception() 需要一个参数,随便填什么都可以,我习惯用 error

logger.exception 和 logger.error 不一样。前者会做变量跟踪,但是后者不会
效果展示:

  1. from loguru import logger
  2. def func(a: int, b: int):
  3. a/b
  4. try:
  5. func(0, 0)
  6. except Exception as error:
  7. logger.exception(error)
  1. ─➤ python -u "/Users/bot/Desktop/code/ideaboom/test_logger/003.py"
  2. 2022-01-09 23:44:12.792 | ERROR | __main__:<module>:11 - division by zero
  3. Traceback (most recent call last):
  4. > File "/Users/bot/Desktop/code/ideaboom/test_logger/003.py", line 9, in <module>
  5. func(0, 0)
  6. <function func at 0x104a4c040>
  7. File "/Users/bot/Desktop/code/ideaboom/test_logger/003.py", line 5, in func
  8. a/b
  9. 0
  10. 0
  11. ZeroDivisionError: division by zero

从图中可以看到,日志展示时候,打印了变量 a 和 b 的值
python 使用 loguru 输出异常日志同时打印变量值
链式异常
loguru 对链式异常(raise … from …)的支持也很好

如果你不知道什么是异常链,可以看 Python 官方文档——异常链

  1. from loguru import logger
  2. def func(a: int, b: int):
  3. try:
  4. a/b
  5. except Exception as error:
  6. raise Exception('计算错误') from error
  7. try:
  8. func(0, 0)
  9. except Exception as error:
  10. logger.exception(error)
  1. ─➤ python -u "/Users/bot/Desktop/code/ideaboom/test_logger/003.py"
  2. 2022-01-09 23:43:04.729 | ERROR | __main__:<module>:14 - 计算错误
  3. Traceback (most recent call last):
  4. File "/Users/bot/Desktop/code/ideaboom/test_logger/003.py", line 6, in func
  5. a/b
  6. 0
  7. 0
  8. ZeroDivisionError: division by zero
  9. The above exception was the direct cause of the following exception:
  10. Traceback (most recent call last):
  11. > File "/Users/bot/Desktop/code/ideaboom/test_logger/003.py", line 12, in <module>
  12. func(0, 0)
  13. <function func at 0x1046e0040>
  14. File "/Users/bot/Desktop/code/ideaboom/test_logger/003.py", line 8, in func
  15. raise Exception('计算错误') from error
  16. Exception('计算错误')
  17. Exception: 计算错误

python 使用 loguru 输出异常日志同时打印变量值
以上就是全部内容,希望对大家有帮助。