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

简介

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XerCis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值