轻量级结构化日志库,提供彩色终端输出和上下文绑定
Project description
dictlog
受 structlog 启发的轻量级结构化日志库,提供彩色终端输出和上下文绑定。
相比 structlog,dictlog 的方法签名完全标注了类型,编辑器可以正常补全和提示。
功能肯定没有 structlog 强大,但是终于不用再看编辑器的错误提示了。
由于是基于系统库 logging 实现,使用上完全兼容。
特性
- 结构化日志 + key-value 上下文绑定(
bind/unbind) - 彩色终端输出,紧凑格式
- 支持 6 种日志级别:
TRACE<DEBUG<INFO<WARNING<ERROR<CRITICAL - 支持子 logger(用
.分隔名称) exception()方法自动附加异常堆栈- 开箱即用,零配置
- 完整的类型标注,编辑器友好
安装
dictlog 在 PyPI 上已经有别人申请了,但是我也没想到更好的名字,所以就先通过 git 安装吧。
pip install git+https://github.com/uiautodev/dictlog.git@x.x.x
# 或使用 uv
uv add git+https://github.com/uiautodev/dictlog.git@x.x.x
要求 Python >= 3.9。
使用
import dictlog
# 基本用法
log = dictlog.get_logger("myapp")
# 调整日志级别,与 logging.DEBUG 等价,默认是 WARNING
log.level = dictlog.DEBUG
# 支持的日志级别:TRACE(5) < DEBUG(10) < INFO(20) < WARNING(30) < ERROR(40) < CRITICAL(50)
log.trace("detailed debug info", user_id=123) # 最详细的调试信息
log.debug("debug message", port=8080)
log.info("server started", port=8080)
# 绑定上下文,后续调用自动携带
log = log.bind(user="alice")
log.info("user logged in") # 自带 user=alice
log = log.unbind("user")
log.info("context removed") # 不再包含 user
dictlog 是如何调用 logging 的
log = dictlog.get_logger("foo", name=123)
log.info("hello")
等价于
root_log = logging.getLogger("dictlog")
if not root_log.handlers:
handler = logging.StreamHandler()
handler.setFormatter(_formatter) # ColorFormatter
root_log.addHandler(handler)
log = logging.getLogger("dictlog.foo")
log.info("hello %s", "name=123")
如果不想使用
dictlog.开头,可以通过修改dictlog._ROOT_NAME实现
开发
# 安装依赖
uv sync
# 运行 pre-commit hooks
uv run pre-commit run --all-files
# 运行示例
uv run dictlog.py
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dictlog-0.1.0.tar.gz.
File metadata
- Download URL: dictlog-0.1.0.tar.gz
- Upload date:
- Size: 100.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80c70768ef639bf4e64821e2ddbefba6972a68cee2de2c45b61b256f3dcef91c
|
|
| MD5 |
ee3e1ae6815e9aa03eb003fec3dfd991
|
|
| BLAKE2b-256 |
b9850a143d754d2d76aa35a0b2ef538f0f2e37fccfaf1d7869497965e7ed591b
|
Provenance
The following attestation bundles were made for dictlog-0.1.0.tar.gz:
Publisher:
publish.yml on uiautodev/dictlog
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dictlog-0.1.0.tar.gz -
Subject digest:
80c70768ef639bf4e64821e2ddbefba6972a68cee2de2c45b61b256f3dcef91c - Sigstore transparency entry: 1399516640
- Sigstore integration time:
-
Permalink:
uiautodev/dictlog@9d8147ef72df2eea2d9effc1867d1bc600de432f -
Branch / Tag:
refs/tags/0.2.1 - Owner: https://github.com/uiautodev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9d8147ef72df2eea2d9effc1867d1bc600de432f -
Trigger Event:
release
-
Statement type:
File details
Details for the file dictlog-0.1.0-py3-none-any.whl.
File metadata
- Download URL: dictlog-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f923d74fba9f8ee0106ac70a16f3b9c7128e1617af325070d46cd4b4339a90de
|
|
| MD5 |
62f4396d69b364497a7f4f1a31217c86
|
|
| BLAKE2b-256 |
32ba2d60235fba930e513591a8847e9b3bf57eeac66a1c7a75b78a2127793b2c
|
Provenance
The following attestation bundles were made for dictlog-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on uiautodev/dictlog
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dictlog-0.1.0-py3-none-any.whl -
Subject digest:
f923d74fba9f8ee0106ac70a16f3b9c7128e1617af325070d46cd4b4339a90de - Sigstore transparency entry: 1399516646
- Sigstore integration time:
-
Permalink:
uiautodev/dictlog@9d8147ef72df2eea2d9effc1867d1bc600de432f -
Branch / Tag:
refs/tags/0.2.1 - Owner: https://github.com/uiautodev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9d8147ef72df2eea2d9effc1867d1bc600de432f -
Trigger Event:
release
-
Statement type: