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 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_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
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
Built Distribution
File details
Details for the file fastapi_and_logging-0.0.1.tar.gz
.
File metadata
- Download URL: fastapi_and_logging-0.0.1.tar.gz
- Upload date:
- Size: 4.5 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d5a6d1aa85a675d66a48b336a62eeb9b186010e254c0ed7762f7781cfc5d9df |
|
MD5 | 3a9a16396d995a8456f5035e1db81ccd |
|
BLAKE2b-256 | 6a8eb6bd67365b10c42a3a4be09c7bee3ae1c7f5fa9438d5cead428d4501c440 |
File details
Details for the file fastapi_and_logging-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: fastapi_and_logging-0.0.1-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.11.2 Linux/6.1.0-13-amd64
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d16128d4c0e4a73f5c7fac01fcdfc8e57af13bcc338e84c4a2ba8c7c5228b2f5 |
|
MD5 | 174141bf40547c01cb33a7dbc85bb2c2 |
|
BLAKE2b-256 | fe14907de24117e848883ff8417e084ddb8f3f7b09441678cdf270352a637ac8 |