Skip to main content

dictlog

English

structlog 启发的轻量级结构化日志库,提供彩色终端输出和上下文绑定。

相比 structlog,dictlog 的方法签名完全标注了类型,编辑器可以正常补全和提示。

功能肯定没有 structlog 强大,但是终于不用再看编辑器的错误提示了。

由于是基于系统库 logging 实现,使用上完全兼容。

Demo

特性

  • 结构化日志 + key-value 上下文绑定(bind/unbind
  • 彩色终端输出,紧凑格式
  • 支持 6 种日志级别:TRACE < DEBUG < INFO < WARNING < ERROR < CRITICAL
  • 支持子 logger(用 . 分隔名称)
  • exception() 方法自动附加异常堆栈;所有日志方法均支持 exc_info*args%-style 格式化)和 stacklevel 参数,与标准 logging 保持一致
  • 开箱即用,零配置
  • 完整的类型标注,编辑器友好

安装

pip install dictlog
# 或使用 uv
uv add dictlog

要求 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)

# % 风格格式化参数,与 logging 用法一致
log.info("hello %s", "world")
log.debug("x=%d y=%d", 1, 2)

# 自定义 stacklevel(默认 1,即直接调用方的帧)
def my_wrapper():
    log.info("from wrapper", stacklevel=2)  # 上报到调用 my_wrapper() 的代码行

# 绑定上下文,后续调用自动携带
log = log.bind(user="alice")
log.info("user logged in")          # 自带 user=alice
log = log.unbind("user")
log.info("context removed")         # 不再包含 user

# 捕获异常并附加堆栈,与 logging 用法一致
try:
    1 / 0
except ZeroDivisionError:
    log.error("something went wrong", exc_info=True)
    # 或使用 exception(),默认 exc_info=True
    log.exception("something went wrong")

默认输出格式

终端默认输出紧凑格式,消息及上下文均为纯文本,不包含 logName:

[I 250811 12:00:00 main.py:42] user logged in user=alice
  • I 为级别首字母,按级别着色:TRACE 灰、DEBUG 青、INFO 绿、WARNING 黄、ERROR 红、CRITICAL 品红
  • 250811 12:00:00 为时间戳,main.py:42 为调用位置
  • 消息和 k=v 上下文均为纯文本,不做任何着色

如果需要显示 logName,使用 ColorFormatter(show_name=True) 自定义 formatter:

import logging
import dictlog

root = logging.getLogger(dictlog._ROOT_NAME)
root.handlers.clear()
handler = logging.StreamHandler()
handler.setFormatter(dictlog.ColorFormatter(show_name=True))
root.addHandler(handler)

输出示例:

[I:myapp 250811 12:00:00 main.py:42] user logged in user=alice

输出到日志文件

dictlog 与 logging.basicConfig 完全兼容。通过 basicConfig 输出到文件时,日志内容为纯文本(包含 key-value 字段,但不包含颜色码):

import logging
import dictlog

# 配置输出到文件
logging.basicConfig(
    level=dictlog.INFO,
    filename="app.log",
    format="%(levelname)s - %(message)s"
)

log = dictlog.get_logger("myapp")
log.info("user logged in", user="alice", ip="192.168.1.1")

文件 app.log 中的内容:

INFO - user logged in user=alice ip=192.168.1.1

dictlog 会同时输出到文件(通过 basicConfig)和终端(带颜色)。如果只需要文件输出,可以禁用 dictlog 的默认 handler。

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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dictlog-0.5.0.tar.gz (103.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dictlog-0.5.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file dictlog-0.5.0.tar.gz.

File metadata

  • Download URL: dictlog-0.5.0.tar.gz
  • Upload date:
  • Size: 103.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for dictlog-0.5.0.tar.gz
Algorithm Hash digest
SHA256 24b36729bcd65b5851aef17a0162d5868d238444950e87b90019aec1a9b710a5
MD5 485065289bdc8d1625f5b468293dd45c
BLAKE2b-256 c338d79b6e164cd60fb01f31daffb5312c82a94d1d5626e0c4066017a8ab2183

See more details on using hashes here.

Provenance

The following attestation bundles were made for dictlog-0.5.0.tar.gz:

Publisher: publish.yml on uiautodev/dictlog

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dictlog-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: dictlog-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for dictlog-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d30c192b4125afe4998a8f664f981ac4aa184497484b12968d38b67d622d14c9
MD5 05513daae03dd3dd527216392d5ef7bb
BLAKE2b-256 dca4fc9bec3a62dbd2f5cdb5b6838a9cc10919793daac15743801f6d696724e5

See more details on using hashes here.

Provenance

The following attestation bundles were made for dictlog-0.5.0-py3-none-any.whl:

Publisher: publish.yml on uiautodev/dictlog

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 files

0.4.2

2 files

0.4.1

2 files

0.4.0

2 files

0.3.0

2 files

0.2.2

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page