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.1.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.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for fastapi_logging_middleware-0.1.1.tar.gz
Algorithm Hash digest
SHA256 08f0049301e329ea407d074b95668144e5bc142e1b32bedebd70cd216bb38696
MD5 7a1292ba91503812728286b2eed3b0b7
BLAKE2b-256 67b716a335be86d41b6a874a8bfb5261321c6eb30080d3766b2fbc095229c33b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_logging_middleware-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8b44aeefa30e9327a48a781a29504db1359c6c180df3ddee7e1e93ad9796962f
MD5 a39fd75dba4fe043cd75b9b662b552cc
BLAKE2b-256 8fd759a5da58cb2d8e920702bac6ff67449ec0e705a858be36d2f377372f5cad

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