Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

feat: Add built-in JsonLogHandler for structured JSON logging

Đang mở
#234 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
48/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
observability-sre

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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)
Ngôn ngữ chính
Python
Star
0
Fork
2
Merge trung bình
10 giờ 37 phút
Pull request đã merge (30 ngày)
8

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của RedHatQE/python-simple-logger

Tất cả issue của RedHatQE/python-simple-logger

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.