Skip to main content

app-log-json

This is a public-source project — see CONTRIBUTING.md for the PR policy.

Typed, non-blocking JSON application logging setup: a JSONFormatter, a dictConfig-building helper, and a setup_logging() that wires a QueueHandler/QueueListener so logging never blocks the calling thread.

Library code must never call setup_logging() on import — only an application's entrypoint should call it.

Quickstart

uv add app-log-json
from app_log_json import build_config, get_logger, setup_logging

setup_logging(build_config("my_app"))
logger = get_logger("my_app")

logger.info("started", extra={"version": "1.2.3"})

Install

uv add app-log-json

For local development against a sibling checkout instead of the published package:

uv add --editable "../app-log-json"

Usage

from app_log_json import build_config, get_logger, setup_logging

setup_logging(build_config("my_app"))
logger = get_logger("my_app")

logger.info("started", extra={"version": "1.2.3"})
  • setup_logging(None) — stderr-only, no file handler, filesystem-safe.
  • setup_logging(build_config(...)) — stderr (text) + a Hive-partitioned JSON Lines file at <log_dir>/year=YYYY/month=MM/day=DD/<app_name>.log.jsonl, one file per UTC day.
  • setup_logging(Path("logging.json")) — load a JSON dictConfig file; a "()": "app_log_json.JSONFormatter" or "()": "app_log_json.HiveDailyFileHandler" reference resolves because the library is importable. The handler takes base_dir (a path string is fine) and filename.

Log location

build_config() picks the log directory in this order:

  1. The log_dir= argument, if given.
  2. The LOG_DIR environment variable, if set (~ is expanded).
  3. <project root>/.local/logs/, where the project root is the nearest ancestor of the current working directory containing pyproject.toml or .git (falling back to the CWD itself).

So an app run anywhere inside, say, ~/git/project logs to ~/git/project/.local/logs/ by default. Add .local/ to the app's .gitignore.

Unhandled exceptions

setup_logging() installs a sys.excepthook that logs any exception which escapes the program as CRITICAL (via the root logger, so it goes through the same JSON file handler) before calling through to the previous hook. KeyboardInterrupt is left alone. This only covers exceptions that would otherwise crash the main thread — wrap background threads in their own try/except and call logger.exception(...) explicitly.

The terminal still shows exactly the traceback an unmodified Python program would print, and nothing extra: the crash record carries a marker that the console handler filters out, so the traceback isn't printed twice. A hand-written config that wants the same behaviour should add the filter to its console handler:

"filters": {"exclude_crash_reports": {"()": "app_log_json.ExcludeCrashReports"}},
"handlers": {"stderr": {"filters": ["exclude_crash_reports"], "…": "…"}}

Development

make test
make pre-commit-all

Metadata

Release files for app-log-json 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for app-log-json 0.3.0
File Size Uploaded Provenance
app_log_json-0.3.0.tar.gz 11.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for app-log-json 0.3.0
File Interpreter ABI Platform
app_log_json-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.2 kB

Release files / app_log_json-0.3.0.tar.gz

Download URL app_log_json-0.3.0.tar.gz
Size 11.3 kB
Tags Source
SHA-256 checksum
How to use checksums
2c46265efc74ec66bb8e5c253f1355371fdf67fce5380ba8cd8ca69c2e65004b
BLAKE2b-256 checksum
How to use checksums
5bfa58412c10e17451b086d7808a30aae97c80faef7d423f2b21f5efdc3f512e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.12 {"installer":{"name":"uv","version":"0.12.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / app_log_json-0.3.0-py3-none-any.whl

Download URL app_log_json-0.3.0-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b20affce722cf94e474ead4eb3698d24bbecd29d0f8ad3d3aaee7d8b84469471
BLAKE2b-256 checksum
How to use checksums
cdf1a8ce14d29ff8fa649f434f793dcb0e12085e2fa79e7ec554c56a55e21ac4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.12 {"installer":{"name":"uv","version":"0.12.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for app-log-json 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page