Skip to main content

Drop-in structured JSON logging for Python using the stdlib logging module

Project description

philiprehberger-json-logger

Tests PyPI version License

Drop-in structured JSON logging for Python using the stdlib logging module.

Install

pip install philiprehberger-json-logger

Usage

Quick setup

from philiprehberger_json_logger import setup

setup(level="DEBUG")

import logging
logging.info("Server started", extra={"port": 8080})
# {"timestamp": "2026-03-13T12:00:00+00:00", "level": "INFO", "message": "Server started", "logger": "root", "module": "app", "line": 5, "port": 8080}

Named logger

import logging
from philiprehberger_json_logger import setup

logger = logging.getLogger("myapp")
setup(level="INFO", logger=logger)

logger.warning("Disk usage high", extra={"usage_pct": 91.3})

Static extra fields

from philiprehberger_json_logger import setup

setup(level="INFO", extra_fields={"service": "api", "env": "production"})

Every log entry will include "service": "api" and "env": "production".

Custom handler with JsonFormatter

import logging
from philiprehberger_json_logger import JsonFormatter

handler = logging.FileHandler("app.log")
handler.setFormatter(JsonFormatter(extra_fields={"service": "worker"}))

logger = logging.getLogger("worker")
logger.addHandler(handler)
logger.setLevel(logging.INFO)

API

Name Description
JsonFormatter(*, extra_fields=None) Logging formatter that outputs JSON lines. extra_fields is a dict of static fields merged into every entry.
setup(level="INFO", *, extra_fields=None, logger=None) Configure a logger with JSON output. Defaults to the root logger. Clears existing handlers.

JSON output fields

Field Description
timestamp ISO 8601 UTC timestamp
level Log level name (DEBUG, INFO, WARNING, ERROR, CRITICAL)
message Formatted log message
logger Logger name
module Source module name
line Source line number
exception Full traceback string (only present when logging an exception)

Any extra={} kwargs passed to the log call are merged into the top-level JSON object. Static extra_fields from the formatter are also merged.

Development

pip install -e .
python -m pytest tests/ -v

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_json_logger-0.1.3.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_json_logger-0.1.3-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_json_logger-0.1.3.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_json_logger-0.1.3.tar.gz
Algorithm Hash digest
SHA256 9a8e478e29b8f61961fbabee09b03a408b03d14dcc1896e622fbc37860970ecc
MD5 f2c2f79156c45a94b72dc9d8ed7c914a
BLAKE2b-256 408dd5f2c9030d49d387af137bc5aa9f37269151e5d2273a6b37c9b32f2501f9

See more details on using hashes here.

File details

Details for the file philiprehberger_json_logger-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_json_logger-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8c58af61b877183b14d9a5dcebd4392bbd66c0727c9d7e5c1ad8fc0c5acc0718
MD5 e872309688af7b94b0bb1d0572ab6ccc
BLAKE2b-256 8022015cd2d6d6675a6e4809cf989f90426777ea113d6c8794207725dac00389

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page