Skip to main content

Advanced logging module for FastAPI - Designed for Solautyc Team internal use

Project description

log2fast-fastapi

๐Ÿš€ Advanced logging module for FastAPI with file rotation, colored output, and environment-based auto-configuration

[!WARNING] Internal Use Notice

This package is designed and maintained by the Solautyc Team for internal use. While it is publicly available, it may not work as expected in all environments or use cases outside of our specific infrastructure. We do not provide support or guarantees for external usage, and we are not responsible for any issues that may arise from using this package in other contexts.

Use at your own risk. Contributions and feedback are welcome, but compatibility with external environments is not guaranteed.

Features

  • ๐ŸŽจ Multiple Output Formats: JSON (production), Colored (development), Structured (debugging), Simple (testing)
  • ๐ŸŒ Environment-Based Configuration: Automatic setup for dev, test, prod, and debug
  • ๐Ÿ“ฆ Module-Based Loggers: Each module gets its own logger instance
  • ๐Ÿ”„ File Rotation: Automatic log file rotation with configurable size
  • ๐Ÿ”„ Environment-Specific Logging: Control which logs appear in which environments (prevent sensitive data leaks)
  • ๐Ÿš€ FastAPI Integration: Middleware for automatic request/response logging with unique request IDs
  • ๐Ÿ“Š Structured Logging: Add context data to any log message
  • ๐Ÿ“Š Context Injection: Support for request_id, user_id, and custom context data
  • ๐ŸŽฏ Zero Configuration: Works out of the box with sensible defaults

๐Ÿ“š Documentation

Installation

From PyPI (Recommended)

pip install log2fast-fastapi

From Source

# Clone the repository
git clone https://github.com/AngelDanielSanchezCastillo/log2fast-fastapi.git
cd log2fast-fastapi

# Install in development mode
pip install -e .

# Or install with dev dependencies
pip install -e ".[dev]"

Quick Start

Basic Usage

from log2fast_fastapi import get_logger

logger = get_logger(__name__)

logger.info("Application started")
logger.warning("This is a warning")
logger.error("An error occurred")

FastAPI Integration

from fastapi import FastAPI
from log2fast_fastapi import RequestLoggingMiddleware, get_logger

app = FastAPI()
app.add_middleware(RequestLoggingMiddleware)

logger = get_logger(__name__)

@app.get("/")
async def root():
    logger.info("Root endpoint accessed")
    return {"message": "Hello World"}

Logging with Context

from log2fast_fastapi import get_logger

logger = get_logger(__name__)

logger.info(
    "User logged in",
    extra_data={
        "user_id": "12345",
        "ip_address": "192.168.1.1"
    }
)

Environment-Specific Logging (Prevent Sensitive Data Leaks!)

# Logs ONLY in development/debug (NOT in production)
logger.debug(
    "Sensitive debug info",
    extra_data={"password_hash": "...", "token": "..."},
    only_in=["development", "debug"]
)

# Logs ONLY in production
logger.info(
    "Performance metrics",
    extra_data={"response_time": 120},
    only_in=["production"]
)

Configuration

Simple: Just set the environment in .env

# That's it! Format and level auto-configure
LOG_ENVIRONMENT=production

Auto-configuration by environment:

Environment Auto Level Auto Format
development INFO colored
production WARNING json
testing INFO simple
debug DEBUG colored

Optional: Override defaults

# Optional: Override auto-configuration
LOG_LEVEL=DEBUG
LOG_FORMAT=json
LOG_FILE_SETTINGS__ENABLED=true

Environment Presets

Development

  • Format: Colored console output
  • Level: INFO
  • Perfect for local development

Production

  • Format: JSON (structured)
  • Level: WARNING
  • Optimized for log aggregation tools

Testing

  • Format: Simple
  • Level: INFO
  • Minimal output for tests

Debug

  • Format: Colored
  • Level: DEBUG
  • Maximum verbosity

Documentation

See docs/usage.md for complete documentation including:

  • Advanced configuration
  • Custom formatters
  • Best practices
  • Integration examples

Example

Run the example application:

python log2fast_fastapi/example.py

Then visit:

Testing

Run the test suite:

python log2fast_fastapi/tests/test_logging.py

Module Structure

log2fast-fastapi/
โ”œโ”€โ”€ pyproject.toml       # Package configuration
โ”œโ”€โ”€ MANIFEST.in          # Additional files to include
โ”œโ”€โ”€ README.md            # This file
โ”œโ”€โ”€ LICENSE              # License file
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ log2fast_fastapi/
โ”‚       โ”œโ”€โ”€ __init__.py          # Main exports
โ”‚       โ”œโ”€โ”€ __version__.py       # Version information
โ”‚       โ”œโ”€โ”€ base.py              # Core FastLogger class
โ”‚       โ”œโ”€โ”€ settings.py          # Configuration with Pydantic
โ”‚       โ”œโ”€โ”€ formatters.py        # Custom log formatters
โ”‚       โ””โ”€โ”€ middleware.py        # FastAPI middleware
โ”œโ”€โ”€ docs/
โ”‚   โ”œโ”€โ”€ usage.md                 # Complete documentation
โ”‚   โ”œโ”€โ”€ file_management.md       # File rotation guide (EN)
โ”‚   โ”œโ”€โ”€ file_management_es.md    # File rotation guide (ES)
โ”‚   โ”œโ”€โ”€ logger_best_practices.md # Best practices
โ”‚   โ””โ”€โ”€ publishing.md            # PyPI publishing guide
โ”œโ”€โ”€ examples/
โ”‚   โ”œโ”€โ”€ example.py               # Basic example
โ”‚   โ”œโ”€โ”€ demo_features.py         # Feature demonstrations
โ”‚   โ””โ”€โ”€ demo_rotation.py         # Rotation examples
โ””โ”€โ”€ tests/
    โ”œโ”€โ”€ test_logging.py          # Test suite
    โ””โ”€โ”€ test_new_features.py     # Feature tests

License

Same as parent project.

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

log2fast_fastapi-0.1.0.tar.gz (34.0 kB view details)

Uploaded Source

Built Distribution

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

log2fast_fastapi-0.1.0-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file log2fast_fastapi-0.1.0.tar.gz.

File metadata

  • Download URL: log2fast_fastapi-0.1.0.tar.gz
  • Upload date:
  • Size: 34.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for log2fast_fastapi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d0f571d0c92fae534d5310682807cfd0b7c46096d1905d385fbd894de7f082ae
MD5 c4d27d0b55c76e9f528d788518fe7cb6
BLAKE2b-256 aa17b95aadf4ef94158afb057fbbb37cd5b1fe425607f4f2c90914fafd6ce28e

See more details on using hashes here.

File details

Details for the file log2fast_fastapi-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for log2fast_fastapi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 64198ba6179894590c1bb626a4f5bafe6643e87194e013a99f2433f3134945b0
MD5 3ab951b0a41ae7a2db5f4efa9375e3cd
BLAKE2b-256 24d5527871faee562ed6e09c1fb88d2d1431206ede43a33e9d39f7893b4c063f

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