Skip to content

πŸͺ΅ Logging & Debug

Good exception handling is only half the battle β€” logging is what keeps your team sane in production.

With APIException, unexpected errors don’t just return a nice JSON response;

they’re also automatically logged so you always have a clear trail of what went wrong.


βœ… How It Works

When you use:

from APIException import register_exception_handlers
from fastapi import FastAPI

app = FastAPI()
register_exception_handlers(
    app=app,
    use_fallback_middleware=True
)
You get two powerful behaviors:

1️⃣ All handled APIExceptions are logged with: - HTTP status - Exception code - Message - Context & traceback

2️⃣ Unhandled exceptions (like DB errors, 3rd-party failures) are caught by the fallback middleware and: - Return a consistent JSON error response (ISE-500 by default) - The full traceback to your console or logging system


βš™οΈ Example Output

When something unexpected happens, you’ll see logs like:

Log-Format


⚑ Tips

βœ… Use FastAPI’s native logging module to pipe logs to your file, console, or external log aggregator (ELK, CloudWatch, etc.).

βœ… Combine this with FastAPI middlewares or your own log formatter if you want structured JSON logs.

βœ… For sensitive environments, make sure your logs do not expose user data.


πŸ“š Next

βœ”οΈ Want to see how fallback works?
Check out πŸͺ“ Fallback Middleware

βœ”οΈ Need better Swagger docs?
Go to πŸ“š Swagger Integration

βœ”οΈ Haven’t defined custom codes yet?
Read πŸ—‚οΈ Custom Exception Codes