feat: Add built-in JsonLogHandler for structured JSON logging
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ả
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)
- 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
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của RedHatQE/python-simple-logger
-
Dependency DashboardĐang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 20/100
RedHatQE/python-simple-logger#130 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 35/100
RedHatQE/python-simple-logger#104 ·
-
Dependency DashboardĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 35/100
Tất cả issue của RedHatQE/python-simple-logger
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 60/100
521xueweihan/HelloGitHub#3924 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100
wilbowes/EchoMuse#869 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
-
Claiming namespace `jft63`Đang mởnamespace operations
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
EclipseFdn/open-vsx.org#14043 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
test: TestServeUntilStale races the server's close against the client's sendall (BrokenPipeError under load)Có thể đã có người làm @evoludigit đã nhận hôm nay. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 89/100
Maintainer thường phản hồi trong vòng 1 ngày