# FastAPI Colorful Logging Middleware
[](https://www.python.org/downloads/)
[](https://fastapi.tiangolo.com/)
[](https://opensource.org/licenses/MIT)
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:
```bash
pip install fastapi-logging-middleware
```
## Quick Start
### Method 1: Using add_middleware (Recommended)
```python
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"}
```
### Method 2: Using middleware parameter in FastAPI
```python
from fastapi import FastAPI
from starlette.middleware import Middleware
from logging_middleware import GoStyleLoggingMiddleware
# Add middleware during app creation
app = FastAPI(
middleware=[
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](LICENSE) file for details.
Raw data
{
"_id": null,
"home_page": null,
"name": "fastapi-logging-middleware",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.11",
"maintainer_email": null,
"keywords": "colorful, fastapi, go-style, logging, middleware",
"author": null,
"author_email": "AnthonyHol <kholkin.anthony@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/58/6d/02f47dd41973ce44efef23131a8cb064a992b18de154dab3c6ec850f1b4d/fastapi_logging_middleware-0.1.2.tar.gz",
"platform": null,
"description": "# FastAPI Colorful Logging Middleware \n[](https://www.python.org/downloads/)\n[](https://fastapi.tiangolo.com/)\n[](https://opensource.org/licenses/MIT)\n\nA 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.\n\nThe 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.\n\n## Installation\n\nInstall the package using pip:\n\n```bash\npip install fastapi-logging-middleware\n```\n\n## Quick Start\n\n### Method 1: Using add_middleware (Recommended)\n\n```python\nfrom fastapi import FastAPI\nfrom logging_middleware import GoStyleLoggingMiddleware\n\napp = FastAPI()\n\n# Add the middleware\napp.add_middleware(GoStyleLoggingMiddleware)\n\n@app.get(\"/\")\nasync def root():\n return {\"message\": \"Hello World\"}\n```\n\n### Method 2: Using middleware parameter in FastAPI\n\n```python\nfrom fastapi import FastAPI\nfrom starlette.middleware import Middleware\nfrom logging_middleware import GoStyleLoggingMiddleware\n\n# Add middleware during app creation\napp = FastAPI(\n middleware=[\n Middleware(GoStyleLoggingMiddleware)\n ]\n)\n\n@app.get(\"/\")\nasync def root():\n return {\"message\": \"Hello World\"}\n```\n\n\n## Example Output\n\n\n## Log Format\n\nThe middleware outputs logs in the following format:\n\n```\nYYYY-MM-DD HH:MM:SS.ffffff [STATUS] | DURATION | CLIENT_IP | [METHOD] \"PATH?QUERY\"\n```\n\n## Color Coding\n\n- **Status Codes:**\n - 2xx: Green background\n - 3xx: Yellow background \n - 4xx: Red background\n - 5xx: Magenta background\n\n- **HTTP Methods:**\n - GET: Blue background\n - POST: Green background\n - PUT: Yellow background\n - DELETE: Red background\n - PATCH: Cyan background\n - HEAD: Magenta background\n - OPTIONS: White background\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Beautiful, colorful HTTP request logs in Go-style format for FastAPI applications",
"version": "0.1.2",
"project_urls": {
"Homepage": "https://github.com/AnthonyHol/fastapi-logging-middleware",
"Issues": "https://github.com/AnthonyHol/fastapi-logging-middleware/issues",
"Repository": "https://github.com/AnthonyHol/fastapi-logging-middleware"
},
"split_keywords": [
"colorful",
" fastapi",
" go-style",
" logging",
" middleware"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "3a06bfcb55a19b5d85f6a8e85713addd77dba43fcd7fd5ce26e9aac637788c4c",
"md5": "b30bbdf9b4841bff8c50e42dfd34c216",
"sha256": "304197f9a7bc0a811f257d43b24ac4e76d66819b6f355e12166008b6f156587a"
},
"downloads": -1,
"filename": "fastapi_logging_middleware-0.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b30bbdf9b4841bff8c50e42dfd34c216",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.11",
"size": 5446,
"upload_time": "2025-07-22T18:47:45",
"upload_time_iso_8601": "2025-07-22T18:47:45.537927Z",
"url": "https://files.pythonhosted.org/packages/3a/06/bfcb55a19b5d85f6a8e85713addd77dba43fcd7fd5ce26e9aac637788c4c/fastapi_logging_middleware-0.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "586d02f47dd41973ce44efef23131a8cb064a992b18de154dab3c6ec850f1b4d",
"md5": "b9200209e1be25efe6a04d6079e3219f",
"sha256": "d59531dfde50887888b76d6909cf18a3983ede88ea94581e4b10a25c98f342bf"
},
"downloads": -1,
"filename": "fastapi_logging_middleware-0.1.2.tar.gz",
"has_sig": false,
"md5_digest": "b9200209e1be25efe6a04d6079e3219f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.11",
"size": 73497,
"upload_time": "2025-07-22T18:47:47",
"upload_time_iso_8601": "2025-07-22T18:47:47.105800Z",
"url": "https://files.pythonhosted.org/packages/58/6d/02f47dd41973ce44efef23131a8cb064a992b18de154dab3c6ec850f1b4d/fastapi_logging_middleware-0.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-22 18:47:47",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "AnthonyHol",
"github_project": "fastapi-logging-middleware",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "fastapi-logging-middleware"
}