当前位置: 首页 > 工具软件 > Eliot > 使用案例 >

Python日志库Eliot——强大日志系统:性能瓶颈、什么时候发生、谁调用了什么

唐弘和
2023-12-01

简介

Eliot,一款强大的日志系统,告诉你:性能瓶颈、什么时候发生、谁调用了什么




安装

pip install eliot eliot-tree

eliot-tree:可视化Eliot日志




初试

import requests
from eliot import start_action, to_file
to_file(open("linkcheck.log", "w"))


def check_links(urls):
    with start_action(action_type="check_links", urls=urls):
        for url in urls:
            try:
                with start_action(action_type="download", url=url):
                    response = requests.get(url)
                    response.raise_for_status()
            except Exception as e:
                raise ValueError(str(e))

try:
    check_links(["http://eliot.readthedocs.io", "http://nosuchurl"])
except ValueError:
    print("Not all links were valid.")

linkcheck.log

{"urls": ["http://eliot.readthedocs.io", "http://nosuchurl"], "action_status": "started", "timestamp": 1595411823.472086, "task_uuid": "532571b5-59a1-4fe8-983b-ed35101c9dfc", "action_type": "check_links", "task_level": [1]}
{"url": "http://eliot.readthedocs.io", "action_status": "started", "timestamp": 1595411823.472086, "task_uuid": "532571b5-59a1-4fe8-983b-ed35101c9dfc", "action_type": "download", "task_level": [2, 1]}
{"action_status": "succeeded", "timestamp": 1595411823.726474, "task_uuid": "532571b5-59a1-4fe8-983b-ed35101c9dfc", "action_type": "download", "task_level": [2, 2]}
{"url": "http://nosuchurl", "action_status": "started", "timestamp": 1595411823.726474, "task_uuid": "532571b5-59a1-4fe8-983b-ed35101c9dfc", "action_type": "download", "task_level": [3, 1]}
{"errno": null, "exception": "requests.exceptions.ConnectionError", "reason": "HTTPConnectionPool(host='nosuchurl', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000000000B398320>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))", "action_status": "failed", "timestamp": 1595411826.2962916, "task_uuid": "532571b5-59a1-4fe8-983b-ed35101c9dfc", "action_type": "download", "task_level": [3, 2]}
{"exception": "builtins.ValueError", "reason": "HTTPConnectionPool(host='nosuchurl', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000000000B398320>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed',))", "action_status": "failed", "timestamp": 1595411826.2962916, "task_uuid": "532571b5-59a1-4fe8-983b-ed35101c9dfc", "action_type": "check_links", "task_level": [4]}

树状可视化

eliot-tree linkcheck.log

效果

b1cb58cf-2c2f-45c0-92b2-838ac00b20cc
└── check_links/1 ⇒ started
    ├── timestamp: 2017-10-27 20:42:47.206684
    ├── urls:
    │   ├── 0: http://eliot.readthedocs.io
    │   └── 1: http://nosuchurl
    ├── download/2/1 ⇒ started
    │   ├── timestamp: 2017-10-27 20:42:47.206933
    │   ├── url: http://eliot.readthedocs.io
    │   └── download/2/2 ⇒ succeeded
    │       └── timestamp: 2017-10-27 20:42:47.439203
    ├── download/3/1 ⇒ started
    │   ├── timestamp: 2017-10-27 20:42:47.439412
    │   ├── url: http://nosuchurl
    │   └── download/3/2 ⇒ failed
    │       ├── errno: None
    │       ├── exception: requests.exceptions.ConnectionError
    │       ├── reason: HTTPConnectionPool(host='nosuchurl', port=80): Max retries exceeded with url: / (Caused by NewConnec…
    │       └── timestamp: 2017-10-27 20:42:47.457133
    └── check_links/4 ⇒ failed
        ├── exception: builtins.ValueError
        ├── reason: HTTPConnectionPool(host='nosuchurl', port=80): Max retries exceeded with url: / (Caused by NewConnec…
        └── timestamp: 2017-10-27 20:42:47.457332

PS:本人在生成树这步报错 ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type




参考文献

  1. Quickstart — Eliot documentation
  2. 哪些 Python 库让你相见恨晚?
  3. itamarst/eliot: Eliot: the logging system that tells you why it happened
  4. jonathanj/eliottree: Render Eliot logs as an ASCII tree
 类似资料: