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

Acknowledgments

This project uses the following open-source packages:

  • FastAPI - Modern, fast web framework for building APIs (MIT License)
  • Pydantic - Data validation using Python type annotations (MIT License)
  • Pydantic Settings - Settings management using Pydantic (MIT License)

We are grateful to the maintainers and contributors of these projects for their excellent work.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Copyright (c) 2026 Angel Daniel Sanchez Castillo

Note: This package is designed and maintained by the Solautyc Team for internal use. While publicly available under MIT license, use at your own risk.

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.2.tar.gz (34.3 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.2-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: log2fast_fastapi-0.1.2.tar.gz
  • Upload date:
  • Size: 34.3 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.2.tar.gz
Algorithm Hash digest
SHA256 964b3243bcde1bba0aa045449a0e7d3f51e130e74e421688cfce8aac86fbfb42
MD5 4dcf44a27071193488594458365b0599
BLAKE2b-256 0ef5ecab2e594bb2e35485becac0bf32bc60445dc3b5c353ed6116b839010595

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for log2fast_fastapi-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 42b7af9fd71c80b6b1a5faf0a0c51870009d36649f7270c93b0bf023ebc8fd8a
MD5 ab4e0bf8abfb9c25ee2b0279d2deb896
BLAKE2b-256 c2e7d266c2ff0dbc0b375e42edc9016a7c7dea0227c5e97f212642ee429d8d0d

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