Hone logo
Hone
Problems

Building a Request Logging Middleware in Python

Middleware in web frameworks provides a powerful way to intercept and process requests before they reach your application logic, and also to process responses before they are sent back to the client. This challenge asks you to create a simple request logging middleware in Python using Flask, demonstrating how to log incoming requests and their associated data. This is a common and useful pattern for debugging, auditing, and monitoring web applications.

Problem Description

You are tasked with creating a Flask middleware component that logs information about each incoming HTTP request. The middleware should:

  1. Intercept Requests: Intercept every incoming request to the Flask application.
  2. Log Request Details: Log the following information for each request:
    • Timestamp (current date and time)
    • Request Method (e.g., GET, POST)
    • Request Path (the URL path)
    • Request Headers (as a dictionary)
    • Request Body (if the request method is POST, PUT, or PATCH; otherwise, log "No body")
  3. Continue Request Processing: After logging, the middleware should not modify the request or response; it should allow the request to proceed to the next handler in the Flask application.
  4. Logging Destination: Log the request details to the console (standard output).
  5. No Impact on Application Logic: The middleware should not alter the behavior of the core Flask application routes.

Edge Cases to Consider:

  • Requests with no body (e.g., GET requests).
  • Large request bodies (consider potential performance implications, though a full solution to this is not required).
  • Error handling (minimal error handling is expected; focus on the core logging functionality).

Examples

Example 1:

Input:
GET request to /hello with headers: {'User-Agent': 'MyAgent'}
Output:
2024-10-27 10:00:00 - GET - /hello - Headers: {'User-Agent': 'MyAgent'} - No body

Example 2:

Input:
POST request to /submit with headers: {'Content-Type': 'application/json'} and body: '{"name": "John", "age": 30}'
Output:
2024-10-27 10:01:00 - POST - /submit - Headers: {'Content-Type': 'application/json'} - Body: {"name": "John", "age": 30}

Example 3: (Edge Case - Empty Body)

Input:
POST request to /empty with headers: {'Content-Type': 'application/json'} and body: ''
Output:
2024-10-27 10:02:00 - POST - /empty - Headers: {'Content-Type': 'application/json'} - Body: ''

Constraints

  • Framework: Flask (version 2.x or higher)
  • Logging: Log to standard output (console).
  • Body Handling: Handle POST, PUT, and PATCH requests. For other methods, log "No body".
  • Performance: The middleware should not introduce significant performance overhead. While optimizing for extremely large bodies is not required, avoid unnecessary operations.
  • Error Handling: Basic error handling is acceptable (e.g., catching exceptions during body parsing). Detailed error reporting is not required.
  • Dependencies: Only use built-in Python libraries and Flask. No external logging libraries are allowed.

Notes

  • You'll need to create a Flask application and define a simple route to test your middleware.
  • The request body should be accessed using request.get_data(). Be mindful of the content type when handling the body.
  • Consider using Python's datetime module for generating timestamps.
  • The goal is to demonstrate the concept of middleware; a production-ready logging system would likely involve more sophisticated features (e.g., file logging, structured logging, filtering).
  • Focus on clarity and readability of your code. Good commenting is appreciated.
Loading editor...
python