DEV Community

Rajath Kumar K S
Rajath Kumar K S

Posted on

1

Creating a Middleware in FastAPI for Logging Request and Responses

FastAPI is a modern, high-performance web framework for Python. One of its powerful features is the ability to customize behavior using middlewares. In this blog, we will walk through creating a middleware to log every request and response, along with useful metadata like the client's IP address, HTTP method, endpoint, and status codes.

FastAPI Logo

Why Use Middleware for Logging?

Middleware in FastAPI is a hook that gets executed for every request and response. This makes it an ideal place to:

  • Log Client Details: Capture IP addresses, user agents, etc.
  • Monitor API Usage: Track requests and responses.
  • Debugging and Auditing: Maintain a log of all interactions with your application.

Setting Up FastAPI

Before we begin, ensure you have FastAPI installed. If not, install it using:

pip install fastapi[all]
Enter fullscreen mode Exit fullscreen mode

Let’s create a basic FastAPI app:

from fastapi import FastAPI
app = FastAPI()

@app.get("/")
async def read_root():
    return {"message": "Welcome to FastAPI!"}
Enter fullscreen mode Exit fullscreen mode

Creating the Middleware

Here's how to create a middleware for logging.

1. Import Required Modules

We'll use Python's built-in logging module to log data into a file.

import logging
from fastapi import FastAPI, Request
from starlette.middleware.base import BaseHTTPMiddleware
Enter fullscreen mode Exit fullscreen mode

2. Configure Logging

Set up a log file and format the logs.

logging.basicConfig(
    filename="app.log",
    level=logging.INFO,
    format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
Enter fullscreen mode Exit fullscreen mode

3. Define the Middleware

Create a custom middleware class that processes every request and response.

class LoggingMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request: Request, call_next):
        # Log request details
        client_ip = request.client.host
        method = request.method
        url = request.url.path

        logger.info(f"Request: {method} {url} from {client_ip}")

        # Process the request
        response = await call_next(request)

        # Log response details
        status_code = response.status_code
        logger.info(f"Response: {method} {url} returned {status_code} to {client_ip}")

        return response
Enter fullscreen mode Exit fullscreen mode

4. Add Middleware to the App

Integrate the middleware with your FastAPI app:

import logging
from fastapi import FastAPI, Request
from starlette.middleware.base import BaseHTTPMiddleware

# Configure logging
logging.basicConfig(
    filename="app.log",
    level=logging.INFO,
    format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)

# Create FastAPI app
app = FastAPI()

# Define logging middleware
class LoggingMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request: Request, call_next):
        # Log request details
        client_ip = request.client.host
        method = request.method
        url = request.url.path

        logger.info(f"Request: {method} {url} from {client_ip}")

        # Process the request
        response = await call_next(request)

        # Log response details
        status_code = response.status_code
        logger.info(f"Response: {method} {url} returned {status_code} to {client_ip}")

        return response

# Add middleware to the app
app.add_middleware(LoggingMiddleware)

# Define a sample route
@app.get("/")
async def read_root():
    return {"message": "Welcome to FastAPI!"}

@app.get("/items/{item_id}")
async def read_item(item_id: int):
    return {"item_id": item_id}
Enter fullscreen mode Exit fullscreen mode

Testing the Middleware

1. Run the FastAPI App: Start the application by running the script:

uvicorn app:app --reload
Enter fullscreen mode Exit fullscreen mode

2. Send Requests: Use tools like curl, Postman, or a browser to interact with the API. For example:

curl http://127.0.0.1:8000/
curl http://127.0.0.1:8000/items/42
Enter fullscreen mode Exit fullscreen mode

3. Check the Logs: Open the app.log file to see the logged requests and responses. It will look something like this:

2024-12-04 12:05:00,001 - INFO - Request: GET / from 127.0.0.1
2024-12-04 12:05:00,002 - INFO - Response: GET / returned 200 to 127.0.0.1
2024-12-04 12:06:00,003 - INFO - Request: GET /items/42 from 127.0.0.1
2024-12-04 12:06:00,004 - INFO - Response: GET /items/42 returned 200 to 127.0.0.1
Enter fullscreen mode Exit fullscreen mode

Conclusion

With this middleware, you can easily log all API requests and responses in your FastAPI application. This is incredibly useful for debugging, auditing, and monitoring the usage of your APIs. You can further customize the middleware to log headers, request bodies, or response payloads if needed.

Logging is an essential part of any production-grade application, and FastAPI’s middleware support makes it easy to implement.

Creating a Middleware in FastAPI for Logging Requests and Responses | Python & Rest

Learn how to create a custom middleware in FastAPI to log all incoming requests and outgoing responses, including client IP and other details, into a log file.

favicon blog.python.rest

Follow me on LinkedIn

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay