πͺ΅ 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
)
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:
β‘ 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