feat: Add built-in JsonLogHandler for structured JSON logging
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 48/100
- Issue 类型
- 功能
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- python
调研方向
Start with the proposed simple_logger.handlers.JsonLogHandler location and compare the reference implementation in webhook_server/utils/json_log_handler.py. Check the existing logger and handler structure before deciding how the callback or ContextVar enrichment fits. Done means JSONL output has the specified fields, date-based files, ANSI stripping, safe append fallback, and failure handling, with tests covering the supported behavior.
由索引模型根据 Issue 内容生成。
描述
Feature Request
Problem
When using python-simple-logger in applications that need structured JSON logging (e.g., webhook servers, API services), there's no built-in way to emit log records as JSON. Users must implement their own logging.Handler subclass to write JSON entries alongside the existing text and console outputs.
Proposed Solution
Add a JsonLogHandler to python-simple-logger that:
- Subclasses
logging.Handlerand writes each log record as a compact JSON line (JSONL format) to date-based log files (e.g.,app_YYYY-MM-DD.json) - Strips ANSI escape codes from messages before serialization
- Uses atomic file append with
fcntl.flock()(with graceful fallback on platforms withoutfcntl) - Supports enrichment via a user-provided callback or
ContextVarfor adding request-scoped metadata (e.g., request ID, user, correlation ID) - Never crashes the application — uses
self.handleError(record)on failures
Example Usage
from simple_logger.logger import get_logger
from simple_logger.handlers import JsonLogHandler # proposed location
logger = get_logger(name="my_app", filename="app.log")
# Attach JSON handler for structured output
json_handler = JsonLogHandler(log_dir="/var/log/myapp", level=logging.INFO)
logger.addHandler(json_handler)
logger.info("Request processed")
# Writes to /var/log/myapp/app_2026-03-15.json:
# {"timestamp": "2026-03-15T10:30:00+00:00", "level": "INFO", "logger_name": "my_app", "message": "Request processed"}
JSON Entry Format
{"timestamp": "ISO8601", "level": "INFO", "logger_name": "my_app", "message": "Log message here"}
Motivation
We implemented this handler in github-webhook-server#1030 to enable a log viewer web UI to display individual log lines with proper levels. Having this built into python-simple-logger would:
- Eliminate the need for downstream projects to maintain their own JSON handler implementations
- Provide a consistent JSON logging format across projects using
python-simple-logger - Complement the existing text file and console handlers with a structured output option
Reference Implementation
A working implementation exists at:
https://github.com/myk-org/github-webhook-server/blob/fix/issue-1030-log-level-json/webhook_server/utils/json_log_handler.py
Key design decisions from the reference:
- ~140 lines, zero external dependencies beyond stdlib
- Atomic append with
fcntl.flock()for safe concurrent writes - Pre-compiled ANSI stripping regex for performance
- Date-based file rotation (
app_YYYY-MM-DD.json)
- 主要语言
- Python
- 星标
- 0
- 派生
- 2
- 平均合并
- 8 小时 27 分钟
- 30 天内合并 PR
- 8
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
RedHatQE/python-simple-logger 的其他 Issue
-
难度 1/5 1 小时以内 新手友好度 20/100
RedHatQE/python-simple-logger#130 ·
-
难度 2/5 1-3 小时 新手友好度 35/100
RedHatQE/python-simple-logger#104 ·
-
难度 2/5 1-3 小时 新手友好度 35/100
查看 RedHatQE/python-simple-logger 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 85/100
mozilla/bedrock#17413 · 1 个 reaction ·
维护者通常 2 天内回复
-
instance instance add
难度 2/5 1-3 小时 新手友好度 68/100
searxng/searx-instances#943 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
bug tools
难度 2/5 1-3 小时 新手友好度 88/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 86/100
lance-format/lance#9655 ·
维护者通常 2 天内回复