Beautiful, colorful HTTP request logs in Go-style format for FastAPI applications
Project description
FastAPI Colorful Logging Middleware
A beautiful, colorful HTTP request logging middleware for FastAPI applications that provides Go-style formatted logs. The middleware automatically captures and logs detailed information about each incoming request, including timing, client IP, HTTP method, and response status with color-coded output.
The middleware extracts client IP addresses from various sources including direct client connection, X-Forwarded-For, and X-Real-IP headers by default. Request duration is automatically calculated and displayed in the most appropriate unit (microseconds, milliseconds, or seconds) for optimal readability.
Installation
Install the package using pip:
pip install fastapi-logging-middleware
Quick Start
from fastapi import FastAPI
from logging_middleware import GoStyleLoggingMiddleware
app = FastAPI()
# Add the middleware
app.add_middleware(GoStyleLoggingMiddleware)
@app.get("/")
async def root():
return {"message": "Hello World"}
Example Output
Log Format
The middleware outputs logs in the following format:
YYYY-MM-DD HH:MM:SS.ffffff [STATUS] | DURATION | CLIENT_IP | [METHOD] "PATH?QUERY"
Color Coding
-
Status Codes:
- 2xx: Green background
- 3xx: Yellow background
- 4xx: Red background
- 5xx: Magenta background
-
HTTP Methods:
- GET: Blue background
- POST: Green background
- PUT: Yellow background
- DELETE: Red background
- PATCH: Cyan background
- HEAD: Magenta background
- OPTIONS: White background
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_logging_middleware-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_logging_middleware-0.1.0.tar.gz
- Upload date:
- Size: 72.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c71dfdc500abc778d3088f0a7e81c877bb33ed405d585de5599aaadd0ab362c
|
|
| MD5 |
7a66a3da060f21c4f42922199a235ac7
|
|
| BLAKE2b-256 |
41febddf20e1add9195088ccd7268825a8ccbd2e4115692c66dd209125bdaedf
|
File details
Details for the file fastapi_logging_middleware-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_logging_middleware-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e13436a12791b381b945af693755c3244be8be4ea6551e0a4866e9bb4498f69a
|
|
| MD5 |
5bb0552fdc9835c633c2d31785814d04
|
|
| BLAKE2b-256 |
7925195930045294ffff5f2b165a6af292b2b2eb71462b7e29488258330245db
|