A FastAPI middleware for logging requests and responses in NDJSON format
Project description
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
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_ndjson_logger-0.1.3.tar.gz.
File metadata
- Download URL: fastapi_ndjson_logger-0.1.3.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d1419d92ec028c833f8873becb3525bebe333783c5669f3de7b4d1407c22e6a
|
|
| MD5 |
4b92cc4a9b5e72eee341d4655a84c0d6
|
|
| BLAKE2b-256 |
6f86910d326e6924ed148d494725ebf5b4c3164ae7cf05845486d9eb605015ca
|
File details
Details for the file fastapi_ndjson_logger-0.1.3-py3-none-any.whl.
File metadata
- Download URL: fastapi_ndjson_logger-0.1.3-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c3c09c9a39d56f9d1aea176d49e0cac4a431fa82c62311a04e1b3de2573722bf
|
|
| MD5 |
15820458ac19dc9cdf9d1df3ada9e6df
|
|
| BLAKE2b-256 |
04e91fcc56f7d93e5b52763feda7320df259ef627f7ab39b32b0aaf51b51969b
|