Skip to main content

A FastAPI middleware for logging requests and responses in NDJSON format

Project description

MIT License

Logo

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.

Installation

Prerequisites

  • Python 3.11 or higher
  • FastAPI framework

Project Setup

fastapi-logger/
├── app/
│   ├── __init__.py
│   ├── main.py                    # FastAPI entry point   ├── middleware/
│      ├── __init__.py
│      ├── fastapi_logging_middleware.py  # Middleware implementation
├── tests/
│   ├── __init__.py
│   ├── test_logging_middleware.py # Unit tests for logging middleware
├── requirements.txt               # Required Python packages
├── README.md                      # Project documentation
├── LICENSE                        # License file

Here's how to set up the project:

  1. Clone the repository:
git clone https://github.com/yourusername/fastapi-logger.git
  1. Set up the virtual environment:
cd fastapi-logger
python -m venv .venv
  1. Activate the virtual environment:
source .venv/bin/activate
  1. Install the project dependencies:
pip install -r requirements.txt

Usage

To use FastAPI-Logger in your FastAPI app & configure the parameters,

from fastapi import FastAPI
from .middleware.fastapi_logging_middleware import RequestResponseLoggingMiddleware
import os

# Create logs directory if it doesn't exist
os.makedirs("logs/request_response_logs", exist_ok=True)


app = FastAPI()
app.add_middleware(
    RequestResponseLoggingMiddleware,
    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

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

Rajath Kumar K S

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

fastapi_ndjson_logger-0.1.1.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_ndjson_logger-0.1.1-py3-none-any.whl (2.8 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_ndjson_logger-0.1.1.tar.gz.

File metadata

  • Download URL: fastapi_ndjson_logger-0.1.1.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.4

File hashes

Hashes for fastapi_ndjson_logger-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b12347d8e5bfe97daf1a085793eccf15d6fee945434f3d8a7ea792ce9e7fd8c0
MD5 33cb515f4227d624b26a530f248677fb
BLAKE2b-256 b8ff03b3ace758d00b022ddfb23368807f9edb7b148be09ce99e8c5584e6ae00

See more details on using hashes here.

File details

Details for the file fastapi_ndjson_logger-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_ndjson_logger-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e2aa47dc99042eafb25b1b1cfe9e5ef62adb3b676886473e6ef68da94a64cc9b
MD5 7a5641e783f5074e09773e7932230e24
BLAKE2b-256 dd9648187a161e5e183ea34a9e833880eb812b87bf8482a84e3f58ae56a55e83

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page