A simple FastAPI application that logs all incoming requests to a file.
- Logs all HTTP requests to separate files per IP address
- Middleware-based request logging with processing time
- Catch-all endpoint that accepts any HTTP method and path
- Health check endpoint
- Detailed request logging including headers, body, and client IP
- IP-based log file separation for better organization
- Install dependencies:
python -m venv .venv
pip install -r requirements.txt
python main.py
Or using uvicorn directly:
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
The API will be available at http://localhost:8000
GET /
- Root endpointGET /health
- Health checkANY /{path:path}
- Catch-all endpoint that logs any request
Requests are logged to separate files based on the client IP address in the logs/
directory:
logs/requests_127_0_0_1.log
- Requests from localhostlogs/requests_192_168_1_100.log
- Requests from 192.168.1.100logs/requests_2001_db8__1.log
- Requests from IPv6 address 2001:db8::1
Each log file includes:
- Timestamp
- HTTP method and URL
- Client IP address
- Request headers
- Query parameters
- Request body (for POST/PUT/PATCH requests)
- Response status code
- Processing time
# Test the API
curl http://localhost:8000/
curl -X POST http://localhost:8000/test -d '{"key": "value"}'
curl http://localhost:8000/any/path/here
Check the logs/
directory to see IP-specific log files with all logged requests.