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

Method 1: Using add_middleware (Recommended)

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

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

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.2.tar.gz (73.5 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.2-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for fastapi_logging_middleware-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d59531dfde50887888b76d6909cf18a3983ede88ea94581e4b10a25c98f342bf
MD5 b9200209e1be25efe6a04d6079e3219f
BLAKE2b-256 586d02f47dd41973ce44efef23131a8cb064a992b18de154dab3c6ec850f1b4d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_logging_middleware-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 304197f9a7bc0a811f257d43b24ac4e76d66819b6f355e12166008b6f156587a
MD5 b30bbdf9b4841bff8c50e42dfd34c216
BLAKE2b-256 3a06bfcb55a19b5d85f6a8e85713addd77dba43fcd7fd5ce26e9aac637788c4c

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