This project provides a flexible and extensible logging framework for Python applications. It offers a clean interface for consistent logging with rich context information, built on top of the Loguru library while maintaining a contract-based approach for easy adaptation to other logging backends.
Key features:
- π Standardized logging interface through a contract
- π Easy integration into existing classes via a mixin
- π Beautiful, colorized console output via Loguru
- π§© Structured logging with context support
- π Granular log level control
- π§ͺ Thoroughly tested implementation
- Python 3.13 or higher
- uv for dependency management
-
Clone the repository
-
Install dependencies
make init
# Create a logger instance
logger = LoguruLogger(level=LogLevel.DEBUG)
# Basic logging
logger.info("Application started")
# Logging with context
logger.debug("Processing item",
context={"item_id": "12345", "status": "pending"})
# Error logging
try:
result = 1 / 0
except Exception as e:
logger.exception("Division error occurred", exc=e,
context={"operation": "division"})# Create a class with logging capabilities
class MyService(LoggableMixin):
def __init__(self, logger: LoggerContract):
super().__init__()
self.logger = logger
def process(self, data):
self.logger.info("Processing data", context={"data_size": len(data)})
# Usage
logger = LoguruLogger(level=LogLevel.INFO)
service = MyService(logger)
service.process([1, 2, 3])Available log levels (from lowest to highest priority):
LogLevel.DEBUG- Detailed information for debuggingLogLevel.INFO- General information about system operationLogLevel.WARNING- Indication of potential issuesLogLevel.ERROR- Error conditions preventing a function from workingLogLevel.CRITICAL- Critical conditions requiring immediate attention
Set the log level when creating the logger:
# Only show warnings and above
logger = LoguruLogger(level=LogLevel.WARNING)The abstract base class that defines the interface for all logger implementations:
| Method | Description |
|---|---|
debug(message, context=None) |
Log debug message with optional context |
info(message, context=None) |
Log info message with optional context |
warning(message, context=None) |
Log warning message with optional context |
error(message, context=None) |
Log error message with optional context |
critical(message, context=None) |
Log critical message with optional context |
exception(message, exc, context=None) |
Log exception with message and optional context |
An implementation of LoggerContract using
the Loguru library.
A mixin class that adds logging capabilities to any class.
| Property | Description |
|---|---|
logger |
Get or set the logger instance |
An enum representing log levels:
DEBUGINFOWARNINGERRORCRITICAL
With a helper method:
from_str(value)- Create LogLevel from a string (case-insensitive)
This project uses ruff for formatting and linting:
# Format code
make format
# Run linters
make lint# Run tests with coverage
make testWe welcome contributions to this project! Please see the CONTRIBUTING.md file for guidelines on how to contribute, including:
- How to set up your development environment
- Coding standards and style guidelines
- Pull request process
- Testing requirements
This project is licensed under the GNU General Public License v3.0 (GPL-3.0) - see the LICENSE file for details.
GPL-3.0 is a strong copyleft license that requires anyone who distributes your code or a derivative work to make the source available under the same terms.