0
0
FastAPIframework~10 mins

Logging errors in FastAPI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Logging errors
Start FastAPI app
Receive request
Process request
Error occurs?
NoSend response
Yes
Log error details
Send error response
Wait for next request
This flow shows how FastAPI handles a request, logs errors if they happen, and sends responses.
Execution Sample
FastAPI
from fastapi import FastAPI, Request
import logging
from fastapi.responses import JSONResponse

app = FastAPI()

@app.exception_handler(Exception)
async def log_exceptions(request: Request, exc: Exception):
    logging.error(f"Error on {request.url}: {exc}")
    return JSONResponse(status_code=500, content={"detail": "Internal Server Error"})
This code sets up FastAPI to catch all exceptions, log the error with request info, and return a simple error message.
Execution Table
StepActionInput/ConditionLogging OutputResponse Sent
1Start FastAPI appN/ANo logs yetN/A
2Receive requestGET /items/1No logs yetN/A
3Process requestCode runs normallyNo logs yet200 OK with item data
4Receive requestGET /items/999 (nonexistent)No logs yetN/A
5Process requestError: Item not foundError on http://localhost/items/999: Item not found500 Internal Server Error
6Send error responseAfter logging errorError logged{"detail": "Internal Server Error"}
7Wait for next requestN/ALogs savedN/A
💡 Execution stops waiting for next request after sending response.
Variable Tracker
VariableStartAfter Step 2After Step 5Final
request.urlN/Ahttp://localhost/items/1http://localhost/items/999http://localhost/items/999
excN/AN/AItem not foundItem not found
logging outputEmptyEmptyError on http://localhost/items/999: Item not foundError on http://localhost/items/999: Item not found
Key Moments - 2 Insights
Why do we see no logs when the request succeeds?
Because logging.error is only called when an exception occurs, as shown in step 3 vs step 5 in the execution_table.
What does the exception handler catch?
It catches all exceptions during request processing, logs them, and returns a generic error response, as seen in steps 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what is logged?
ANo logs yet
BError on http://localhost/items/999: Item not found
C200 OK with item data
DInternal Server Error
💡 Hint
Check the Logging Output column at step 5 in the execution_table.
At which step does the app send a 500 Internal Server Error response?
AStep 3
BStep 2
CStep 6
DStep 1
💡 Hint
Look at the Response Sent column for the 500 error in the execution_table.
If no exception occurs, what will the logging output be after processing the request?
ANo logs yet
BError on request URL
CError logged
DInternal Server Error
💡 Hint
Refer to step 3 in the execution_table where the request succeeds without error.
Concept Snapshot
FastAPI error logging:
- Use @app.exception_handler(Exception) to catch errors
- Log error details with logging.error()
- Return a generic error response
- Only logs errors when exceptions occur
- Normal requests do not produce error logs
Full Transcript
This visual execution shows how FastAPI handles requests and logs errors. When a request comes in, the app processes it. If no error happens, it sends a normal response without logging. If an error occurs, the exception handler logs the error with the request URL and exception message. Then it sends a 500 Internal Server Error response with a simple JSON message. Variables like request.url and exc hold the request info and error details. The logging output updates only when an error happens. This helps developers see what went wrong while keeping normal requests quiet.