Skip to main content

log2fast-fastapi

🚀 Advanced logging module for FastAPI with file rotation, colored output, and environment-based auto-configuration

📖 Conventions reference: this package follows the 2fast-handbook for ecosystem conventions (structure, versioning, README, commits, release).

[!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:

  • http://localhost:8000/ - Root endpoint
  • http://localhost:8000/users/123 - User endpoint
  • http://localhost:8000/docs - API documentation

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.

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.3.tar.gz (34.7 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.3-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for log2fast_fastapi-0.1.3.tar.gz
Algorithm Hash digest
SHA256 b1da1975ea90c0381c98668ae9cf65e3820dfd1e9a0f4e1b32a78119670903ad
MD5 39db5682645377440eb3500040f048fb
BLAKE2b-256 e9ebbd903afd24540503b0f69b1bfd49f5b44dcf835e57def1fd99f3c9eb89f4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for log2fast_fastapi-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 30d3c2dec1633d3d53d2a87425f15e3271b24cc4add844e259ba00e6920e9ba5
MD5 9e8410dcc6ccbaf7324aa682cda20c37
BLAKE2b-256 05ca5b253b30c90ed2c2bc7d5790d3d0aa53bb9dd56f70d250445ccbcbeff26b

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page