Production-ready error handling, logging, and monitoring for FastAPI apps
Project description
fastapi-armor 🛡️
Production-ready error handling, logging, and Sentry monitoring for FastAPI — in one line.
Why fastapi-armor?
Every production FastAPI app needs the same boilerplate: structured logging, clean error responses, and error monitoring. fastapi-armor wires all of that up automatically — no copy-pasting, no missing edge cases.
Before:
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse
import logging, logging.handlers, sentry_sdk
# ... 60+ lines of setup code
app = FastAPI()
After:
from fastapi import FastAPI
from fastapi_armor import Armor
app = FastAPI()
Armor(app) # ✅ Done.
Installation
pip install fastapi-armor
Quick Start
from fastapi import FastAPI
from fastapi_armor import Armor
app = FastAPI()
Armor(
app,
log_file="logs/app.log",
sentry_dsn="https://your-dsn@sentry.io/123", # optional
environment="production",
)
@app.get("/")
def root():
return {"status": "running"}
That's it. Your app now has:
- ✅ Rotating file logs + console output
- ✅ Clean JSON error responses for all exception types
- ✅ Sentry integration (if DSN is provided)
What It Does
🔴 Error Handling
| Exception Type | Response |
|---|---|
HTTPException |
{"detail": "..."} with original status code |
RequestValidationError |
{"error": "Validation failed", "detail": [...]} with 422 |
Any other Exception |
{"error": "Internal server error"} with 500 (full traceback logged internally) |
All responses use Content-Type: application/json; charset=utf-8.
📋 Logging
- Rotating file logs — auto-creates the
logs/directory - Console output — for local development
- Format:
2024-01-01 12:00:00 | ERROR | Traceback (most recent call last): ... - Named logger:
fastapi_armor
🔍 Sentry (optional)
Initializes sentry_sdk with FastApiIntegration and StarletteIntegration when a DSN is provided. Private data is not sent by default.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
app |
FastAPI |
required | Your FastAPI application instance |
log_file |
str |
"logs/app.log" |
Path to the rotating log file |
log_max_bytes |
int |
5_000_000 |
Max size per log file (5 MB) |
log_backup_count |
int |
3 |
Number of backup log files to keep |
sentry_dsn |
str |
"" |
Sentry DSN — leave empty to disable |
environment |
str |
"development" |
Environment tag sent to Sentry |
Requirements
- Python 3.9+
- FastAPI 0.100+
- sentry-sdk 1.40+ (installed automatically)
Development
git clone https://github.com/raghadalb-tech/fastapi-armor.git
cd fastapi-armor
pip install -e ".[dev]"
pytest tests/ -v
Test output:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_armor_lib-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_armor_lib-0.1.0.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
462c0bab6f353feee6236cb0b88f234ca478b8e2de251e71dac3b5400d6be426
|
|
| MD5 |
1dbe7e8b5fe7e1ac0f3051a5eeae3027
|
|
| BLAKE2b-256 |
2a7b72a8173e8a447cea3d035ceec0eb1fb32d2528665e21884cf1ce2876e4f2
|
File details
Details for the file fastapi_armor_lib-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_armor_lib-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a2d4580e83eb93a4512fabc48f66ef7c3ed3228ec79dbc80174cc9db8e0f2b8b
|
|
| MD5 |
1d5b26b9d80d35a85241fd706ca46add
|
|
| BLAKE2b-256 |
8b8c7e05b7a7f31245e07575d92fc4e8177fd1a5abcb3660053aebacb0dfe024
|