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:
- 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)
Feature Request
Problem
When using
python-simple-loggerin 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 ownlogging.Handlersubclass to write JSON entries alongside the existing text and console outputs.Proposed Solution
Add a
JsonLogHandlertopython-simple-loggerthat: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)fcntl.flock()(with graceful fallback on platforms withoutfcntl)ContextVarfor adding request-scoped metadata (e.g., request ID, user, correlation ID)self.handleError(record)on failuresExample Usage
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-loggerwould:python-simple-loggerReference 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:
fcntl.flock()for safe concurrent writesapp_YYYY-MM-DD.json)