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 JSONdictConfigfile; a"()": "app_log_json.JSONFormatter"or"()": "app_log_json.HiveDailyFileHandler"reference resolves because the library is importable. The handler takesbase_dir(a path string is fine),filenameand an optionalretention_days.
Log location
build_config() picks the log directory in this order:
- The
log_dir=argument, if given. - The
LOG_DIRenvironment variable, if set (~is expanded). <project root>/.local/logs/, where the project root is the nearest ancestor of the current working directory containingpyproject.tomlor.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.
Retention
Daily partitioning has no size cap, so build_config() sets
retention_days=30: a day's partition is deleted once it is more than 30 days
old. Pass a different number, or retention_days=None to keep everything.
setup_logging(build_config("my_app", retention_days=7))
Pruning is driven by the partition path, not by mtime, so a log tree that
was copied or rsync'd still expires by the day each record was written. It
runs whenever the handler opens a file — the first write of a process and each
crossing of UTC midnight — so a short cron run prunes at startup and a
long-lived daemon prunes as it rolls over. A prune that fails never costs a
log line.
Only <app_name>.log.jsonl is removed from an expired partition, because a
shared LOG_DIR holds other apps' files (with their own retention) beside it;
the day=/month=/year= directories go only once that emptied them.
Directories that don't parse as a partition are never touched.
To prune a tree without logging to it — from a cleanup job, say:
from app_log_json import prune_partitions
prune_partitions("/var/log/my_app", "my_app.log.jsonl", keep_days=30)
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
Releasing
make release-check # preflight guards only — safe to run any time
make build # rm -rf dist, then uv build
make release # guards, test, build, tag, push, publish, GitHub release
First, bump version in pyproject.toml and run uv lock, then land it
via its own PR — pre-commit blocks committing to main. The lock records the
project's own version, so bumping pyproject.toml alone leaves it stale and
every uv run --frozen refuses to start.
Then, with the bump on main, make release tags v<version>, pushes
the tag, publishes to PyPI and cuts the GitHub release. It publishes before
creating the release, so the release only appears once the artifact is live.
make release-check holds the guards, and refuses to proceed unless: on
main, clean tree, in sync with origin, the tag is free both locally and on
origin, the version is absent from PyPI (it can never be re-uploaded), and
UV_PUBLISH_TOKEN is set. Run it alone to check readiness without releasing.
make build always clears dist/ first, because uv publish uploads every
file in that directory — a leftover artifact would otherwise ship too.
Metadata
Release files for app-log-json 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| app_log_json-0.4.0.tar.gz | 16.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| app_log_json-0.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 27.4 kB
Release files / app_log_json-0.4.0.tar.gz
| Download URL | app_log_json-0.4.0.tar.gz |
|---|---|
| Size | 16.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
71092ac926bc915b95a74b882e1e7676000e980bb0efd3ceae9259d620f831ac
|
|
BLAKE2b-256 checksum How to use checksums |
86b3960b1b27363687fed3393cfcd4d8694f019f103eb113bf065a444e80f30c
|
| 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.4.0-py3-none-any.whl
| Download URL | app_log_json-0.4.0-py3-none-any.whl |
|---|---|
| Size | 11.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a2b036e6e808a7b9140a2bbe5fbffb8373c4152c4b0417284685758174d1ed5a
|
|
BLAKE2b-256 checksum How to use checksums |
576ab13a99909a4874881c09e5783f6f01ac175d3b5863c4aff4b94455fb4095
|
| 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.4.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.