ASGI Logger Middleware

0.1.0 · active · verified Thu Apr 16

asgi-logger is a middleware-based access logger designed for ASGI servers, offering an alternative to the default Uvicorn logger. It is compatible with any ASGI application and provides customizable log formats. The current version is 0.1.0, released in November 2021, with development continuing on GitHub though without frequent PyPI releases.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates integrating `asgi-logger` with a FastAPI application. It shows how to add the `AccessLoggerMiddleware` with a custom format and, crucially, how to disable Uvicorn's built-in access logger to prevent redundant log entries.

import logging
from fastapi import FastAPI
from fastapi.middleware import Middleware
from asgi_logger import AccessLoggerMiddleware
import uvicorn

# Instantiate FastAPI app
app = FastAPI(
    middleware=[
        Middleware(AccessLoggerMiddleware, format='%(client_addr)s - "%(request_line)s" %(status_code)s')
    ]
)

@app.get("/hello")
async def home():
    return {"message": "Hello world!"}

# Important: Disable Uvicorn's default access logger to avoid duplicate logs
logging.getLogger("uvicorn.access").handlers = []

if __name__ == "__main__":
    # To run, save this as main.py and execute: uvicorn main:app --port 8000
    print("Navigate to http://127.0.0.1:8000/hello")
    uvicorn.run(app, host="127.0.0.1", port=8000)

view raw JSON →