Skip to content

Commit 9f0440b

Browse files
Merge pull request #1 from inokufu/feature/logger
✨ Logger system
2 parents 4e7c705 + bd8d700 commit 9f0440b

18 files changed

Lines changed: 612 additions & 7 deletions

docs/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,15 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.1.0] - 2025-03-20
9+
10+
### Added
11+
12+
- Initial release of the logging framework
13+
- `LoggerContract` abstract base class defining the logger interface
14+
- `LoguruLogger` implementation using Loguru library
15+
- `LogLevel` enum for standardized log levels
16+
- `LoggableMixin` for adding logging capabilities to classes
17+
- Comprehensive test suite for all components
18+
- Support for structured logging with context information
19+
- Exception logging with type and message extraction

docs/README.md

Lines changed: 134 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,19 @@
1313

1414
## Overview
1515

16-
Python logger
16+
This project provides a flexible and extensible logging framework for Python
17+
applications. It offers a clean interface for consistent logging with rich
18+
context information, built on top of the Loguru library while maintaining a
19+
contract-based approach for easy adaptation to other logging backends.
20+
21+
Key features:
22+
23+
- 📏 Standardized logging interface through a contract
24+
- 🔄 Easy integration into existing classes via a mixin
25+
- 🌈 Beautiful, colorized console output via Loguru
26+
- 🧩 Structured logging with context support
27+
- 📊 Granular log level control
28+
- 🧪 Thoroughly tested implementation
1729

1830
## Setup and installation
1931

@@ -24,18 +36,135 @@ Python logger
2436

2537
### Installation
2638

39+
1. Clone the repository
40+
41+
2. Install dependencies
42+
```sh
43+
make init
44+
```
45+
2746
## Usage
2847

48+
### Basic Usage
49+
50+
```python
51+
# Create a logger instance
52+
logger = LoguruLogger(level=LogLevel.DEBUG)
53+
54+
# Basic logging
55+
logger.info("Application started")
56+
57+
# Logging with context
58+
logger.debug("Processing item",
59+
context={"item_id": "12345", "status": "pending"})
60+
61+
# Error logging
62+
try:
63+
result = 1 / 0
64+
except Exception as e:
65+
logger.exception("Division error occurred", exc=e,
66+
context={"operation": "division"})
67+
```
68+
69+
### Using the LoggableMixin
70+
71+
```python
72+
# Create a class with logging capabilities
73+
class MyService(LoggableMixin):
74+
def __init__(self, logger: LoggerContract):
75+
super().__init__()
76+
self.logger = logger
77+
78+
def process(self, data):
79+
self.logger.info("Processing data", context={"data_size": len(data)})
80+
81+
82+
# Usage
83+
logger = LoguruLogger(level=LogLevel.INFO)
84+
service = MyService(logger)
85+
service.process([1, 2, 3])
86+
```
87+
88+
### Log Levels
89+
90+
Available log levels (from lowest to highest priority):
91+
92+
- `LogLevel.DEBUG` - Detailed information for debugging
93+
- `LogLevel.INFO` - General information about system operation
94+
- `LogLevel.WARNING` - Indication of potential issues
95+
- `LogLevel.ERROR` - Error conditions preventing a function from working
96+
- `LogLevel.CRITICAL` - Critical conditions requiring immediate attention
97+
98+
Set the log level when creating the logger:
99+
100+
```python
101+
# Only show warnings and above
102+
logger = LoguruLogger(level=LogLevel.WARNING)
103+
```
104+
105+
## API Reference
106+
107+
### LoggerContract
108+
109+
The abstract base class that defines the interface for all logger
110+
implementations:
111+
112+
| Method | Description |
113+
|-----------------------------------------|-------------------------------------------------|
114+
| `debug(message, context=None)` | Log debug message with optional context |
115+
| `info(message, context=None)` | Log info message with optional context |
116+
| `warning(message, context=None)` | Log warning message with optional context |
117+
| `error(message, context=None)` | Log error message with optional context |
118+
| `critical(message, context=None)` | Log critical message with optional context |
119+
| `exception(message, exc, context=None)` | Log exception with message and optional context |
120+
121+
### LoguruLogger
122+
123+
An implementation of `LoggerContract` using
124+
the [Loguru](https://github.com/Delgan/loguru) library.
125+
126+
### LoggableMixin
127+
128+
A mixin class that adds logging capabilities to any class.
129+
130+
| Property | Description |
131+
|----------|--------------------------------|
132+
| `logger` | Get or set the logger instance |
133+
134+
### LogLevel
135+
136+
An enum representing log levels:
137+
138+
- `DEBUG`
139+
- `INFO`
140+
- `WARNING`
141+
- `ERROR`
142+
- `CRITICAL`
143+
144+
With a helper method:
145+
146+
- `from_str(value)` - Create LogLevel from a string (case-insensitive)
147+
29148
## Development
30149

31150
### Code Formatting and Linting
32151

33-
### Environment Variables
152+
This project uses ruff for formatting and linting:
153+
154+
```sh
155+
# Format code
156+
make format
157+
158+
# Run linters
159+
make lint
160+
```
34161

35-
| Variable | Description | Required | Default Value | Possible Values |
36-
|----------|-------------|----------|---------------|-----------------|
162+
### Running Tests
37163

38-
### Architecture
164+
```sh
165+
# Run tests with coverage
166+
make test
167+
```
39168

40169
## Contributing
41170

docs/api/loggable_mixin.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# LoggableMixin
2+
3+
::: src.logger.loggable_mixin.LoggableMixin

docs/api/loglevel.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# LogLevel
2+
3+
::: src.logger.loglevel.LogLevel

docs/api/loguru_logger.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# LoguruLogger
2+
3+
::: src.logger.loguru.LoguruLogger
4+

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dependencies = [
1010
"mkdocstrings-python~=1.16.6",
1111
"mkdocs-material~=9.6.9",
1212
"griffe-inherited-docstrings~=1.1.1",
13+
"loguru~=0.7.3",
1314
]
1415
readme = "docs/README.md"
1516
requires-python = ">= 3.12"

requirements-dev.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ jinja2==3.1.6
5252
# via mkdocs
5353
# via mkdocs-material
5454
# via mkdocstrings
55+
loguru==0.7.3
56+
# via logger
5557
markdown==3.7
5658
# via mkdocs
5759
# via mkdocs-autorefs

requirements.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ jinja2==3.1.6
3636
# via mkdocs
3737
# via mkdocs-material
3838
# via mkdocstrings
39+
loguru==0.7.3
40+
# via logger
3941
markdown==3.7
4042
# via mkdocs
4143
# via mkdocs-autorefs

src/logger/__init__.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
"""Package init file."""
1+
"""Logger package for standardized logging across projects."""
2+
3+
from .contract import LoggerContract
4+
from .loggable_mixin import LoggableMixin
5+
from .loglevel import LogLevel
6+
from .loguru import LoguruLogger
7+
8+
__all__ = [
9+
"LogLevel",
10+
"LoggableMixin",
11+
"LoggerContract",
12+
"LoguruLogger",
13+
]

src/logger/__main__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)