Skip to main content

FastAPI MongoDB logging package for endpoints and general logging

Project description

FastAPI MongoDB Logger

A comprehensive logging package for FastAPI applications that stores endpoint logs and custom events in MongoDB.

Installation

pip install fastapi-mongo-logger-mateoramos

Quick Start

1. Basic Setup

from fastapi import FastAPI
from fastapi_mongo_logger import MongoLogger, LoggingMiddleware

app = FastAPI()

# Initialize logger
logger = MongoLogger(
    mongo_url="mongodb://localhost:27017",
    database_name="my_app_logs",
    collection_name="api_logs"
)

# Add middleware for automatic endpoint logging
app.add_middleware(LoggingMiddleware, logger=logger)

@app.get("/")
async def root():
    return {"message": "Hello World"}

2. Manual Logging with Decorators

from fastapi_mongo_logger import log_endpoint, log_function

@log_endpoint(logger, user_type="admin")
async def admin_function():
    # Your code here
    pass

@log_function(logger, event_type="data_processing")
def process_data():
    # Your code here
    pass

3. Custom Logging

# Log custom events anywhere in your code
await logger.log_custom("user_action", {
    "user_id": "123",
    "action": "login",
    "ip_address": "192.168.1.1"
})

Features

  • Automatic endpoint logging via middleware
  • Manual logging with decorators
  • Custom event logging for any part of your application
  • Comprehensive data capture: request/response bodies, headers, timing, errors
  • Async/sync function support
  • Flexible data storage in MongoDB

Configuration Options

  • log_request_body: Enable/disable request body logging (default: True)
  • log_response_body: Enable/disable response body logging (default: False)
  • Custom fields can be added to any log entry

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_mongo_logger_mateoramos-1.0.3.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file fastapi_mongo_logger_mateoramos-1.0.3.tar.gz.

File metadata

File hashes

Hashes for fastapi_mongo_logger_mateoramos-1.0.3.tar.gz
Algorithm Hash digest
SHA256 c82c475ce69eaae7570946a1d605e13d942b6b96e587b7dfe9a9a9bd6d896219
MD5 786c1d080eff214151aff198974ed434
BLAKE2b-256 5df0cea7b58b56e8d525022847ae1adb225eccbc12337c2c17748ba91af9cd95

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_mongo_logger_mateoramos-1.0.3.tar.gz:

Publisher: release.yaml on mateoramos97/fastapi-mongo-logs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastapi_mongo_logger_mateoramos-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_mongo_logger_mateoramos-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c7de00f8c188eea0144e9e96b81df4240e12f85aa574336934c09c53aafc48f7
MD5 0e7d23bd1e4fac7339e8f9eae23832e4
BLAKE2b-256 ac1dd176b07be8a1f6c648fe135388d86b551579e539d631baa30abf0a6236d9

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_mongo_logger_mateoramos-1.0.3-py3-none-any.whl:

Publisher: release.yaml on mateoramos97/fastapi-mongo-logs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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