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-idheader, it uses that value; otherwise, it generates a new UUID. -
Debugging support: Enables debugging of response bodies through the use of the
api_debugflag. When enabled, the response body is included in the logs. -
JSON logging configuration: Utilizes the
python-json-loggerlibrary 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.
Metadata
Release files for logmiddleware 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| logmiddleware-0.0.4.tar.gz | 4.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| logmiddleware-0.0.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.1 kB
Release files / logmiddleware-0.0.4.tar.gz
| Download URL | logmiddleware-0.0.4.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
faa4639c373dcbebaabb23d2cb851490b361bcd435dbf58c93189b9ea69c36c2
|
|
BLAKE2b-256 checksum How to use checksums |
3ce480bc4bca12a650ca687581285798abe6d8276ec73c3dd0251d15613f52a1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.6
|
Release files / logmiddleware-0.0.4-py3-none-any.whl
| Download URL | logmiddleware-0.0.4-py3-none-any.whl |
|---|---|
| Size | 4.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
364f657d5b5d4cf0f077fc18e0b464994b9693e668e58cf17e0b40f0970cc5c1
|
|
BLAKE2b-256 checksum How to use checksums |
f25889afdb2082fbbaab75b1596498259b44fbe132207b825b2ec4b989d5e554
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.6
|
Security for logmiddleware 0.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.