Skip to main content

Beautiful, colorful HTTP request logs in Go-style format for FastAPI applications

Project description

FastAPI Colorful Logging Middleware

Python 3.11+ FastAPI License: 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:

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

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_logging_middleware-0.1.0.tar.gz (72.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_logging_middleware-0.1.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_logging_middleware-0.1.0.tar.gz.

File metadata

File hashes

Hashes for fastapi_logging_middleware-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1c71dfdc500abc778d3088f0a7e81c877bb33ed405d585de5599aaadd0ab362c
MD5 7a66a3da060f21c4f42922199a235ac7
BLAKE2b-256 41febddf20e1add9195088ccd7268825a8ccbd2e4115692c66dd209125bdaedf

See more details on using hashes here.

File details

Details for the file fastapi_logging_middleware-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_logging_middleware-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e13436a12791b381b945af693755c3244be8be4ea6551e0a4866e9bb4498f69a
MD5 5bb0552fdc9835c633c2d31785814d04
BLAKE2b-256 7925195930045294ffff5f2b165a6af292b2b2eb71462b7e29488258330245db

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page