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).
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
- Usage Guide - Comprehensive usage guide with examples
- File Management - Complete guide on log rotation and storage (English)
- Gestión de Archivos - Guía completa de rotación y almacenamiento (Español)
- Logger Best Practices - Best practices for creating and naming loggers
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.
Metadata
Release files for log2fast-fastapi 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| log2fast_fastapi-0.1.3.tar.gz | 34.7 kB | Without provenance | Details for log2fast_fastapi-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| log2fast_fastapi-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for log2fast_fastapi-0.1.3-py3-none-any.whl |
Total release size: 48.7 kB
Release files / log2fast_fastapi-0.1.3.tar.gz
| Download URL | log2fast_fastapi-0.1.3.tar.gz |
|---|---|
| Size | 34.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b1da1975ea90c0381c98668ae9cf65e3820dfd1e9a0f4e1b32a78119670903ad
|
|
BLAKE2b-256 checksum How to use checksums |
e9ebbd903afd24540503b0f69b1bfd49f5b44dcf835e57def1fd99f3c9eb89f4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.9
|
Release files / log2fast_fastapi-0.1.3-py3-none-any.whl
| Download URL | log2fast_fastapi-0.1.3-py3-none-any.whl |
|---|---|
| Size | 14.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
30d3c2dec1633d3d53d2a87425f15e3271b24cc4add844e259ba00e6920e9ba5
|
|
BLAKE2b-256 checksum How to use checksums |
05ca5b253b30c90ed2c2bc7d5790d3d0aa53bb9dd56f70d250445ccbcbeff26b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.9
|
Security for log2fast-fastapi 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.