- If you find a bug, create an issue on the GitHub repository.
- Provide as much detail as possible, including steps to reproduce the issue and any relevant environment details.
If you have an idea for a new feature or an improvement, create a feature request issue.
- Follow these steps to make changes to the codebase:
- Create a new branch for your feature or bug fix:
git checkout -b feature/your-feature-name
- Make your changes, including updates to tests and documentation.
- Run the tests to ensure your changes work as expected:
pytest
- Add or update tests for any new functionality or bug fixes in the tests/ directory.
- Ensure test coverage remains high.
- Push your branch to your forked repository:
git push origin feature/your-feature-name
- Create a pull request (PR) to the main branch of the original repository.
- Provide a clear description of the changes in your PR, including the issue it addresses (if applicable).
- Follow PEP 8 for Python code style.
- Use descriptive variable names and add comments where necessary.
- Format your code with black to ensure consistency:
black .
.
├── CODE_OF_CONDUCT.md
├── LICENSE
├── MANIFEST.in
├── README.md
├── app
│ ├── __init__.py
│ ├── fastapi_ndjson_logger
│ │ ├── __init__.py
│ │ └── fastapi_ndjson_logger.py
│ ├── main.py
│ └── tests
│ ├── __init__.py
│ └── test_logging_middleware.py
├── contributing.md
├── logs
│ └── request_response_logs
│ └── app_log.ndjson
└── requirements.txtHere's how to set up the project:
- Clone the repository:
git clone https://github.com/yourusername/fastapi-logger.git- Set up the virtual environment:
cd fastapi-logger
python -m venv .venv- Activate the virtual environment:
source .venv/bin/activate- Install the project dependencies:
pip install -r requirements.txtTo use FastAPI-Logger in your FastAPI app & configure the parameters,
from fastapi import FastAPI
from .fastapi_ndjson_logger.fastapi_ndjson_logger import (
RequestResponseLogging,
)
import os
# Create logs directory if it doesn't exist
os.makedirs("logs/request_response_logs", exist_ok=True)
app = FastAPI()
app.add_middleware(
RequestResponseLogging,
log_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
)
@app.get("/")
async def root():
return {"message": "Hello World"}
Then run the app using uvicorn app.main:app --reload
| 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 |