Skip to main content

Lean JSON logging with async-safe context, PII redaction, sampling, and EMERGENCY alerts.

Project description

pylogalert

Lean JSON logging for Python with async-safe contextvars, PII redaction, sampling, and EMERGENCY alerts (Slack, SES).
Designed for production services and framework integration (FastAPI, Django).


🚀 Installation

pip install pylogalert
# optional extras
pip install pylogalert[ses]   # enable AWS SES email notifications

🔑 Features

  • Structured JSON logs (NDJSON, one log per line).

  • Context propagation: async-safe via contextvars.

  • PII Redaction: hide sensitive keys or regex patterns.

  • Sampling: reduce noise and cost (info: 0.1, debug: 0.01).

  • New log level: EMERGENCY (≥ CRITICAL).

  • Exception logging: automatic exc_type, exc_message, and stack.

  • Notifications: plugable notifier system with:

  • Slack Webhook (built-in)

  • AWS SES email (optional extra [ses])

  • Rate limiting, deduplication, retries

  • Framework integration: ready to plug into FastAPI middleware or Django logging config.

  • Zero mandatory deps (stdlib only).

📖 Usage

Basic logging

import pylogalert as log

log.configure(service="checkout-api", env="prod")

log.set_context(request_id="req-123")
log.info("order_created", user_id=42, order_id=777, value=129.9)

try:
    1/0
except:
    log.exception("unexpected_error", user_id=42, order_id=777)

Output (colorized if LOG_PRETTY=1 or stream.isatty()):

{"ts":"2025-08-29T12:00:00Z","level":"INFO","service":"checkout-api","env":"prod","event":"order_created","request_id":"req-123","user_id":42,"order_id":777,"value":129.9}
{"ts":"2025-08-29T12:00:00Z","level":"ERROR","service":"checkout-api","env":"prod","event":"unexpected_error","request_id":"req-123","user_id":42,"order_id":777,"exc_type":"ZeroDivisionError","exc_message":"division by zero","stack":"Traceback ..."}

PII redaction

log.configure(service="auth", env="prod",
              redact_keys=["password", "token"],
              redact_regexes=[r"\b\d{3}\.\d{3}\.\d{3}\-\d{2}\b"])  # hide CPF

log.info("user_login", email="a@x.com", password="secret123")

Output:

{"event":"user_login","email":"a@x.com","password":"***REDACTED***"}

Emergency alerts (Slack)

import pylogalert as log
from pylogalert.notify import Notifier
from pylogalert.notify_slack import slack_webhook

slack = slack_webhook("https://hooks.slack.com/services/XXX/YYY/ZZZ")

notifier = Notifier(
    channels=[slack],
    rate_limit=("6/min", 5),
    dedupe_window=60,
    retries=(2, 2.0),
)

log.configure(service="billing", env="prod")
log.emergency("payment_failed", invoice="inv-001", amount=199.0, _notify=notifier)

Emergency alerts (AWS SES)

import pylogalert as log
from pylogalert.notify import Notifier
from pylogalert.notify_ses import ses_email  # requires: pip install pylogalert[ses]

notifier = Notifier(
    channels=[ses_email(sender="alerts@mydomain.com", to=["ops@mydomain.com"])],
    rate_limit=("1/min", 1),
)

log.configure(service="checkout", env="prod")
log.emergency("critical_invariant_broken", account_id=123, _notify=notifier)

⚙️ Environment variables

  • LOG_LEVEL: default log level (default: INFO)

  • APP_ENV: environment tag (default: dev)

  • LOG_PRETTY: enable colors (1, true, yes)

📦 Project status

  • 0.1.0 – first release

  • Semantic Versioning (SemVer)

  • MIT licensed

🛠 Development

git clone https://github.com/jonrato/pylogalert.git
cd pylogalert
python3 -m venv venv && source venv/bin/activate
pip install -e ".[all]"   # install with extras
pytest -q

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

pylogalert-0.1.6.1.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

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

pylogalert-0.1.6.1-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file pylogalert-0.1.6.1.tar.gz.

File metadata

  • Download URL: pylogalert-0.1.6.1.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pylogalert-0.1.6.1.tar.gz
Algorithm Hash digest
SHA256 ef9d9ad5efa327fe5b5d89c70bb619006343b39155fefed15464db00c45ebf78
MD5 afcee6dc939f704760270ad345e4c74a
BLAKE2b-256 b6c3704beb1a679aa0cdc215e3f6f82c7df3109bc98758a898486c0e90e2eb13

See more details on using hashes here.

Provenance

The following attestation bundles were made for pylogalert-0.1.6.1.tar.gz:

Publisher: release.yml on jonrato/pylogalert

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pylogalert-0.1.6.1-py3-none-any.whl.

File metadata

  • Download URL: pylogalert-0.1.6.1-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pylogalert-0.1.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 744bf139223f913f104fe7d9d717d8cf221eb53350f2bc92e2165179daecf875
MD5 59907468f4f301476bf6c2b394082a06
BLAKE2b-256 5b807eb1e1cab4ae27e4d7eedd6ff834ac7e770b18ef373dafe16d8fa1b8fe3e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pylogalert-0.1.6.1-py3-none-any.whl:

Publisher: release.yml on jonrato/pylogalert

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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