Skip to main content

FastAPI-and-Logging simplifies log handling, allowing for effective organization, tracking, and analysis of logs in FastAPI applications, aiding in debugging and issue resolut

Project description

fastapi-and-logging

FastAPI And Logging

FastAPI-and-Logging simplifies log handling, allowing for effective organization, tracking, and analysis of logs in FastAPI applications, aiding in debugging and issue resolution.

Install

pip install fastapi-and-logging

IncomiongLog

This FastAPIIncomingLog class is designed to log incoming system requests using FastAPI.

Parameters:

  • app: It is used to set route_class.
  • request_id_builder: A function to build a request identifier, if specified(It uses uuid4 by default).
  • log_builder: A function used to construct logs.
  • get_request_data: A function used to retrieve request information.
  • get_response_data: A function used to retrieve response information.
  • response_max_len: The maximum length of a response stored in the log (default is 5000).
  • log_path (optional): Log file path.
  • log_type: The type of logging, which can be one of various types (default is LogTypeEnum.FILE).

How to Use:

To use this class, you can create an instance of it and set it as the route_class for your relevant Router in your FastAPI application.

from fastapi import FastAPI
from fastapi_and_logging import FastAPIIncomingLog

app = FastAPI()
FastAPIIncomingLog(app)

Customizing and Using Default Functions

The provided default functions (get_request_data, get_response_data, and log_builder) serve as customizable components for the FastAPIIncomingLog class. Here's how you can customize and use them:

get_request_data

This function is responsible for extracting and formatting request data.

from fastapi_and_logging import Request
from fastapi_and_logging.helpers import get_request_data


async def customize_get_request_data(request: Request):
    # You can also use the output of the default function
    data = await get_request_data(request)
    return data

get_response_data

This function handles the processing of response data.

from fastapi import Response
from fastapi_and_logging.helpers import get_response_data


def customize_get_response_data(response: Response) -> dict | str:
    # You can also use the output of the default function
    data = get_response_data(response)
    return data

log_builder

The log_builder function constructs the log data based on various parameters.

from fastapi import Request, Response
from user_agents.parsers import UserAgent
from fastapi_and_logging.helpers import log_builder


def customize_log_builder(
    request: Request,
    response: Response,
    request_data: dict | str,
    response_data: dict | str,
    user_agent: UserAgent,
    start_time: int,
    end_time: int,
    duration: int,
):
    # You can also use the output of the default function
    data = log_builder(**)
    return data

APICallLog

Note: Currently it only supports httpx.

You can easily log all apicalls using httpx by adding the HTTPXLogger class.

Parameters:

  • request_hook (optional): A callable object that serves as a hook for capturing and logging HTTP requests. It takes a single parameter, the request object, and does not return any value. Defaults to None..
  • response_hook (optional): A callable object that acts as a hook to capture and log HTTP responses. Any value returned will be logged.
  • sync_client (optional): A boolean value indicating whether the logging functionality should be applied to synchronous HTTP clients. If True, the hooks and configuration will be set for synchronous clients. Defaults to True.
  • async_client (optional): A boolean value indicating whether the logging functionality should be applied to asynchronous HTTP clients. If True, the hooks and configuration will be set for asynchronous clients. Defaults to True.
  • request_max_len (optional): An integer specifying the maximum length of the request body to be logged. If the request body exceeds this length, it will be truncated. Defaults to 5000 .
  • response_max_len (optional): An integer specifying the maximum length of the response body to be logged. If the response body exceeds this length, it will be truncated. Defaults to 5000.
  • log_path (optional): Log file path.
  • log_type (optional): Specifies the type of logging, currently it takes two values: console and file.

How to Use:

from fastapi import FastAPI
import httpx
from fastapi_and_logging.http_clients import HTTPXLogger


app = FastAPI()
HTTPXLogger()


@app.get("/")
def index():
    with httpx.Client() as client:
        response = client.get("http://localhost:8000/path")

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_and_logging-0.0.2.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

fastapi_and_logging-0.0.2-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_and_logging-0.0.2.tar.gz.

File metadata

  • Download URL: fastapi_and_logging-0.0.2.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.2 Linux/6.1.0-13-amd64

File hashes

Hashes for fastapi_and_logging-0.0.2.tar.gz
Algorithm Hash digest
SHA256 60d736b5f3d5ffbea5b02b282ffb93d5a650c2ce27aed19896f7fe1241dfb68e
MD5 76e2ceb526ab3fce69272d16c2e117fc
BLAKE2b-256 25041ad2813dec4d90e0fe6932c5db0b705df94b706c94241c590cbc9e9c35f1

See more details on using hashes here.

File details

Details for the file fastapi_and_logging-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_and_logging-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 31e500f6c100b107ff878309c39a2504345c527f2bac5edd45bdb930bde44889
MD5 0f3b71a2c33a9613f25f9effa9014495
BLAKE2b-256 3c5a716c7a1830dc7c4503a132889830259e52998b318e0cb4a21ae734443d87

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page