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
- 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file log2fast_fastapi-0.1.1.tar.gz.
File metadata
- Download URL: log2fast_fastapi-0.1.1.tar.gz
- Upload date:
- Size: 34.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e09e9be059bca44b47317f46e376a61ddc608c12a44bbec7ed222cd134fb727
|
|
| MD5 |
10ad5e52067b6342f3c7c5fc528f7bc1
|
|
| BLAKE2b-256 |
5f35ce784950a9610652b4cf69b0a20ae2e0660dfbe08aab5b9d3bdfb830760d
|
File details
Details for the file log2fast_fastapi-0.1.1-py3-none-any.whl.
File metadata
- Download URL: log2fast_fastapi-0.1.1-py3-none-any.whl
- Upload date:
- Size: 13.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bfaa67bcaf23fef404cea9e646411751203ca3221aa2225bfcf811f4c1b4cc75
|
|
| MD5 |
d1961fb672abb691999e5baf93a97a2a
|
|
| BLAKE2b-256 |
d934d203426c8adea6fc5546aec4677a783ef69d81f16bee67fd883037abd2e5
|