You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

fastapi-logging-middleware

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastapi-logging-middleware

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

0.1.2
pipPyPI
Maintainers
1

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"}

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.

Keywords

colorful

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.