Skip to content

feat: Add built-in JsonLogHandler for structured JSON logging #234

@myakove

Description

@myakove

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions