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
0 commit comments