Skip to main content

nahiarhdlog

PyPI version Python Version License: MIT

Embedded observability for FastAPI: searchable logs, error tracking, alerts, metrics, and tracing — with zero-config defaults.

from fastapi import FastAPI
from nahiarhdLOG import observe

app = FastAPI()
observe(app, dashboard_token="secret")  # logs + errors + metrics + dashboard

Status: FastAPI is supported first. The core is framework-agnostic, so Flask/Django adapters can follow without changing it.

2-minute install

pip install nahiarhdlog

Requires Python >= 3.10. No servers, no agents, no config files: events are stored in a local SQLite file (nahiarhdlog.db).

Quick start

import logging
from fastapi import FastAPI
from nahiarhdLOG import observe

app = FastAPI()
observe(app, dashboard_token="secret")

log = logging.getLogger(__name__)

@app.get("/users/{user_id}")
def get_user(user_id: int):
    log.info("fetching user %d", user_id)
    return {"id": user_id}

That's it. Every request, every stdlib log record, and every uncaught exception is now captured. Open the dashboard at /admin/logs (enter the token on the lock screen).

Features

Area What you get
Logs Auto-captured stdlib records + requests; full-text search (FTS5), level/type/time/trace filters, pagination
Errors Grouped by ExcType@file.py:line, full tracebacks, top-errors ranking
Alerts Threshold rules (N events in M seconds) + cooldown; webhook, Telegram, and SMTP sinks
Metrics RPS, error rate, latency p50/p95, time-bucketed series — computed from the same request events
Tracing One trace_id per request shared by logs and errors; timeline view; optional OpenTelemetry export
Dashboard Embedded at /admin/logs, token-locked, dark/light mode, mobile-friendly, live tail

Design limits (on purpose): bounded in-memory queue (full → drop + visible counter, requests are never blocked), batched async writes, paginated queries. Measured middleware overhead: p95 ~0.04 ms (see scripts/probe_perf.py).

Configuration

from nahiarhdLOG.alerter import Rule, TelegramSink, WebhookSink

observe(
    app,
    db_path="nahiarhdlog.db",   # local SQLite file (WAL mode)
    retention_days=7,            # auto-purge older events
    sample_rate=1.0,             # 1.0 = every request; 500s are always kept
    level=logging.INFO,          # minimum stdlib level captured
    rules=[Rule("errors", count=5, window_seconds=300, cooldown_seconds=3600)],
    sinks=[TelegramSink(bot_token="...", chat_id="...")],
    dashboard_prefix="/admin/logs",
    dashboard_token="secret",    # omit to disable the dashboard entirely
    skip_paths=["/healthz"],     # extra paths the middleware ignores
)

The dashboard's own traffic is never logged, so live tail can't flood itself.

Alert sinks

from nahiarhdLOG.alerter import Rule, SmtpSink, TelegramSink, WebhookSink

rules = [Rule("api-errors", count=10, window_seconds=600, cooldown_seconds=1800)]
sinks = [
    WebhookSink("https://hooks.example/deploy"),          # POSTs {"subject","body"} as JSON
    TelegramSink(bot_token="123:ABC", chat_id="-100..."),  # Bot API sendMessage
    SmtpSink("smtp.example.com", ["ops@example.com"], username="bot", password="..."),
]

Rules evaluate in a background thread; a failing sink never blocks the others.

OpenTelemetry export (optional)

pip install "nahiarhdlog[otel]"
from nahiarhdLOG.otel import export_trace
from nahiarhdLOG.query import get_trace

export_trace(get_trace(collector.storage, trace_id))  # keeps the original trace id

Example app

# terminal 1: run the demo
uv run python examples/basic_app.py serve

# terminal 2: generate traffic
uv run python examples/basic_app.py traffic --n 300

Open http://127.0.0.1:8000/admin/logs with token demo-token.

FAQ

Which Python versions? 3.10+ (3.10, 3.11, 3.12 tested in CI).

Where is data stored? One SQLite file (db_path), WAL mode, FTS5 index for search. Delete the file to wipe everything. Set retention_days for automatic purging.

Is the dashboard secure? It is mounted only when dashboard_token is set, and every page + API call requires the token (query param or Authorization: Bearer). Without it you get a lock screen (401). Use a long random token and HTTPS in production.

Why don't I see the dashboard's own requests in the logs? By design: the dashboard prefix is auto-excluded so its polling doesn't drown your signal. Add more with skip_paths.

What's the overhead? Requests only enqueue a small dict; SQLite writes happen in batches on a background thread. Run python scripts/probe_perf.py to measure on your machine.

Flask / Django / plain scripts? On the roadmap. The core (collector, storage, query, alerter, metrics) imports no web framework — only the thin adapters/ layer does, enforced by an automated boundary test.

How do I disable the dashboard? Omit dashboard_token (default): nothing is mounted.

Roadmap

  • Core logging, SQLite+FTS5 storage, search API
  • Error tracker + webhook alerts
  • Metrics + tracing (+ optional OpenTelemetry export)
  • Embedded dashboard UI
  • Flask/Django adapters
  • Postgres storage backend

Development

uv pip install -e ".[dev,otel]"
uv run --no-sync python -m pytest   # run via python -m (see note below)
uv run --no-sync python scripts/probe_perf.py --n 10000

Note (macOS quirk, this machine): something on this Mac re-applies the Finder's hidden flag to dot-directory contents, which makes CPython silently skip .pth files and breaks console-script entry points in .venv. Running tests via python -m (imports from the source tree) is immune, as are regular installs and CI. If a console script ever reports ModuleNotFoundError, run chflags -R nohidden .venv and prefer python -m.

License

MIT — see LICENSE.

By Raihan Hidayatullah Djunaedi · nahiarhd.com · PyPI @nahiarhd

Metadata

Release files for nahiarhdlog 0.1.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 nahiarhdlog 0.1.0
File Size Uploaded
nahiarhdlog-0.1.0.tar.gz 38.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nahiarhdlog 0.1.0
File Interpreter ABI Platform
nahiarhdlog-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 71.2 kB

Release files / nahiarhdlog-0.1.0.tar.gz

Download URL nahiarhdlog-0.1.0.tar.gz
Size 38.3 kB
Tags Source
SHA-256 checksum
How to use checksums
2f9bbe1c19bf36decda6751c52f9a45ebbcd4f35b9f9e03d88ba092fac4b9573
BLAKE2b-256 checksum
How to use checksums
f83656bc52e0af5aa33c95531a6326101402fdde874d38c42c6dc1f4b8f80a5b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / nahiarhdlog-0.1.0-py3-none-any.whl

Download URL nahiarhdlog-0.1.0-py3-none-any.whl
Size 32.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
76948fa32c33878c48d2746184106f8fddf833e9fe5b0599d198241ef8922a7e
BLAKE2b-256 checksum
How to use checksums
5a832e52f7733fffc971dd1f97915ed6333e7410f6e46368a390de5fd86dcbea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release history Release notifications | RSS feed

0.8.1

2 release files

0.8.0

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.3

2 release files

This release

0.1.0 This release

2 release files

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