FastAPI-Logger
FastAPI-Logger is a middleware that provides easy-to-use request and response logging for FastAPI applications. It supports log rotation, custom log directory, and handles sensitive data securely by redacting sensitive headers.
Features
- Logs all incoming requests and outgoing responses.
- Supports log rotation with customizable file size and backup count.
- Handles sensitive data by redacting headers like
Authorization. - Outputs logs in JSON format for easy integration with log management tools.
- Flexible configuration for log directory and file size.
Requirements
- Python 3.12 or higher
- FastAPI framework
Installation
Create and activate a virtual environment and then install FastAPI & fastapi_ndjson_logger:
pip install "fastapi[standard]" fastapi_ndjson_logger
Example
Create a new FastAPI app and add the middleware to it:
import os
from fastapi import FastAPI
from fastapi_ndjson_logger import RequestResponseLogging
# Create logs directory if it doesn't exist
os.makedirs("logs/request_response_logs", exist_ok=True)
app = FastAPI()
app.add_middleware(
RequestResponseLogging,
og_dir=os.path.join("logs", "request_response_logs"), # Directory for log files
max_mbytes=8, # 8 MB max file size
backup_count=3, # Keep up to 3 rotated files
)
# Add routes to the app
@app.get("/")
async def read_root():
return {"Hello": "World"}
Run it
uvicorn app.main:app --reload
Check the logs
cat logs/request_response_logs/app_log.ndjson
Configuration Parameters
| Parameter | Description | Default Value |
|---|---|---|
log_dir |
Directory to store log files | app_logs |
max_mbytes |
Maximum size of a log file in Mega bytes before rotation | 8 (8 MB) |
backup_count |
Number of rotated files to retain | 5 |
Support
For any questions or issues, feel free to open an issue on the GitHub repository.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Please see the CONTRIBUTING.md for guidelines on how to submit improvements and bug fixes. Also, please see the CODE_OF_CONDUCT.md.
Author
Metadata
Release files for fastapi-ndjson-logger 0.1.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 | |
|---|---|---|---|
| fastapi_ndjson_logger-0.1.4.tar.gz | 7.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_ndjson_logger-0.1.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.8 kB
Release files / fastapi_ndjson_logger-0.1.4.tar.gz
| Download URL | fastapi_ndjson_logger-0.1.4.tar.gz |
|---|---|
| Size | 7.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7d526c9272ff04673db3556e9f28ce8511fe48eafd5b577d5aa8fdc6c9d8cc29
|
|
BLAKE2b-256 checksum How to use checksums |
f27e9e58d60c112a876d8173fd0e833e817203eea179e834e50f2d33b835ce26
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.7
|
Release files / fastapi_ndjson_logger-0.1.4-py3-none-any.whl
| Download URL | fastapi_ndjson_logger-0.1.4-py3-none-any.whl |
|---|---|
| Size | 4.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
56c579ebb02a49fa05b44a147a33cf1d5e70e8624db4b1e9f3c216ef9873c5fa
|
|
BLAKE2b-256 checksum How to use checksums |
7b210e5121d6d3c51b1ec33bbd18aed8f4fb0eb884d7091c6c5c6e197664f737
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.7
|