RedHatQE / RedHatQE/python-simple-logger

feat: Add built-in JsonLogHandler for structured JSON logging

Open
#234 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
0
Forks
2
Avg merge
8h 39m
Merged PRs (30d)
8

Description

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)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

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.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
observability-sre
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.