Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

feat: Add built-in JsonLogHandler for structured JSON logging

未关闭
#234 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 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 内容生成。

描述

enhancement

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.Handler and 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 without fcntl)
  • Supports enrichment via a user-provided callback or ContextVar for 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:

  1. Eliminate the need for downstream projects to maintain their own JSON handler implementations
  2. Provide a consistent JSON logging format across projects using python-simple-logger
  3. 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,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

RedHatQE/python-simple-logger 的其他 Issue

查看 RedHatQE/python-simple-logger 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。