Skip to main content

Health checks, config validation, and structured logging for FastAPI

Project description

stacklink

Health checks, config validation, and structured logging for FastAPI — in three lines of code.

Built for solo devs and small teams who want production-grade infrastructure without the setup overhead.

Install

pip install stacklink

Quick start

from fastapi import FastAPI
from stacklink import Config, Field, health, logger

app = FastAPI()

class AppConfig(Config):
    env: str = Field(required=True)
    service: str = Field(default="my-api")
    port: int = Field(default=8080)
    db_url: str = Field(required=True, secret=True)

config = AppConfig()

logger.configure(config)
logger.info("server starting", port=config.port)

health.register(app, config=config)

That's it. You now have:

  • Validated, typed config from .env and environment variables
  • Structured JSON logging to stdout
  • /healthz and /readyz endpoints on your FastAPI app

Modules

Config

Reads .env files and environment variables. Validates types. Fails loudly at startup if anything is wrong. Redacts secrets from output.

from stacklink import Config, Field

class AppConfig(Config):
    env: str = Field(required=True)
    port: int = Field(default=8080)
    db_url: str = Field(required=True, secret=True)

config = AppConfig()
config.as_dict()  # {"env": "production", "port": 8080, "db_url": "***"}

Supported types: str, int, float, bool. Bool coerces from true/false/1/0/yes/no.

Logger

Structured JSON logging. Every line is machine-parseable. Auto-tags with service name and environment when configured.

from stacklink import logger

logger.info("request handled", method="GET", path="/users", status=200)
# {"timestamp": "...", "level": "INFO", "message": "request handled", "method": "GET", "path": "/users", "status": 200}

Set LOG_LEVEL env var to control filtering (default: INFO).

Health

Registers /healthz (liveness) and /readyz (readiness) endpoints on your FastAPI app.

from stacklink import health

health.register(app, config=config)

async def check_database():
    try:
        await db.execute("SELECT 1")
        return True
    except Exception:
        return False

health.add_check("database", check_database)

/healthz always returns 200. /readyz returns 200 when all checks pass, 503 when any check fails.

Documentation

Requirements

  • Python 3.10+
  • FastAPI 0.100+

Development

pip install -e ".[dev]"
pytest

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

stacklink-0.1.0.tar.gz (207.1 kB view details)

Uploaded Source

Built Distribution

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

stacklink-0.1.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

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