Skip to main content

FastAPI MiddleWare Logger

This library is a very simple tool to simplify custom formatting of logs for FastAPI. It's almost entirely based on this answer from SO.

Usage

The design of this library is made to be very simple to use. Instead of instantiating FastAPI, just instatiate FastAPIMiddleWareLogger:

from fastapi_middleware_logger.fastapi_middleware_logger import add_custom_logger
from fastapi_middleware_logger import FastAPIMiddleWareLogger
import logging
from fastapi import FastAPI


logging.basicConfig(level=logging.INFO)


app = FastAPIMiddleWareLogger()
# app = FastAPI()
# app = add_custom_logger(app)


@app.get("/")
def get_index():
    return {"status": "ok"}


@app.get("/error")
def get_error():
    raise TypeError("Some error happened !")

Another way to add custom loggers is to use add_custom_logger.

Custom logger

If the request is handled properly, the custom_logger will be called. The custom_logger can take the following arguments:

  • request_body: body of the request as a string
  • request_headers: headers of the request as a dictionary
  • request_query_params: query parameters of the request as a dictionary
  • request_method: method of the request as a string
  • request_url: URL of the request as a string
  • response_body: body of the response as a string
  • response_headers: headers of the response as a dictionary
  • response_media_type: media type of the response as a string
  • response_status_code: status code of the response as an integer

By default, the custom_logger will print out all of these arguments.

Custom error logger

If the request throws an exception, the custom_error_logger will be called. The custom_error_logger can take the following arguments:

  • request_body: body of the request as a string
  • request_headers: headers of the request as a dictionary
  • request_query_params: query parameters of the request as a dictionary
  • request_method: method of the request as a string
  • request_url: URL of the request as a string
  • error_message: message of the exception as a string

Usual uvicorn/fastapi logs can be kept by using disable_uvicorn_logging=False. This could be used to perform other operations but I have not, nor will, tested it.

Release files for fastapi-middleware-logger 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-middleware-logger 0.0.5
File Size Uploaded
fastapi-middleware-logger-0.0.5.tar.gz 4.2 kB Details

Release files / fastapi-middleware-logger-0.0.5.tar.gz

Download URL fastapi-middleware-logger-0.0.5.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
53f8889f778e36e2020abc49633574fa1e1babce02aa0854912b19f73b71e012
BLAKE2b-256 checksum
How to use checksums
a98e1488e8df5a617fbfea8f6580780e96faac8adad7c2333c672b5a293ce878
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.0.5 This release

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page