Simplify the logging of incoming requests and outgoing responses.
Project description
logmiddleware
logmiddleware
is a Python library designed for use with FastAPI to simplify the logging of incoming requests and outgoing responses. It includes a middleware component that adds essential information to logs, such as request and response details, and supports debugging features.
Features
-
Middleware for request and response logging: The library provides a middleware component,
RouterLoggingMiddleware
, to log incoming requests and outgoing responses. It includes features to capture request details, response information, and execution times. -
Request ID generation: Automatically generates a unique request ID for each incoming request. If the request includes an
x-api-request-id
header, it uses that value; otherwise, it generates a new UUID. -
Debugging support: Enables debugging of response bodies through the use of the
api_debug
flag. When enabled, the response body is included in the logs. -
JSON logging configuration: Utilizes the
python-json-logger
library to configure JSON logging. This allows for structured and easily parsable log entries.
Installation
To install the library, use the following pip command:
pip install logmiddleware
Usage
Setting up middleware
import logging
from fastapi import FastAPI
from logmiddleware import RouterLoggingMiddleware, logging_config
# Configure JSON logging
logging.config.dictConfig(logging_config)
app = FastAPI()
# Add the middleware to your FastAPI app
app.add_middleware(
RouterLoggingMiddleware,
logger=logging.getLogger(__name__), # Pass your logger instance
api_debug=True, # Set to True to enable debugging of response bodies
)
Accessing request ID in the called function
The execute_request
method has been updated to set the request_id
in the request.state
. You can now access it in the called function using request.state.request_id
. Here is an example:
# ...
@app.get("/example")
async def example_route(request: Request):
# Accessing the request_id in the called function
request_id = request.state.request_id
return {"message": "Hello, world!", "request_id": request_id}
Configuration
The library relies on a logging configuration dictionary (logging_config
) to set up JSON logging. Because of that, it includes a default logging configuration (logging_config
), which you can import for easy setup. Ensure this configuration is properly set before adding the middleware to your FastAPI app.
Contributing
If you find any issues or have suggestions for improvements, please feel free to open an issue or create a pull request on the Git repository.
License
This project is licensed under the MIT license.
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
Hashes for logmiddleware-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 33de06fe12a8cd595a02b7cf40b24c708da533c13970dbebf68ce61510cec813 |
|
MD5 | 3301ab1e552611dca35e0eb985feff1b |
|
BLAKE2b-256 | 6af2ef11fa0b40c911088e9dd09fca5e87705842612c55bef3278ad3b427300f |