easy-logy 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- easy_logy-0.1.0/LICENSE.md +21 -0
- easy_logy-0.1.0/PKG-INFO +238 -0
- easy_logy-0.1.0/README.md +204 -0
- easy_logy-0.1.0/easy_logger/__init__.py +15 -0
- easy_logy-0.1.0/easy_logger/core.py +124 -0
- easy_logy-0.1.0/easy_logger/utils.py +36 -0
- easy_logy-0.1.0/easy_logy.egg-info/PKG-INFO +238 -0
- easy_logy-0.1.0/easy_logy.egg-info/SOURCES.txt +11 -0
- easy_logy-0.1.0/easy_logy.egg-info/dependency_links.txt +1 -0
- easy_logy-0.1.0/easy_logy.egg-info/requires.txt +7 -0
- easy_logy-0.1.0/easy_logy.egg-info/top_level.txt +1 -0
- easy_logy-0.1.0/pyproject.toml +58 -0
- easy_logy-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 aiwonderland
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
easy_logy-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: easy-logy
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A simplified Python logging library with colored console output and automatic file logging
|
|
5
|
+
Author-email: aiwonderland <quantbit@126.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/aiwonderland/easy_logger
|
|
7
|
+
Project-URL: Repository, https://github.com/aiwonderland/easy_logger
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/aiwonderland/easy_logger/issues
|
|
9
|
+
Project-URL: Documentation, https://github.com/aiwonderland/easy_logger#readme
|
|
10
|
+
Keywords: logging,logger,colored-output,file-logging,easy-logging,python-logging
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
22
|
+
Classifier: Topic :: System :: Logging
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.8
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
License-File: LICENSE.md
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
29
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
30
|
+
Requires-Dist: mypy>=1.0.0; extra == "dev"
|
|
31
|
+
Requires-Dist: black>=23.0.0; extra == "dev"
|
|
32
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
|
|
35
|
+
# EasyLogger Usage Guide
|
|
36
|
+
|
|
37
|
+
A simplified Python logging library with colored console output and automatic file logging.
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from easy_logger import EasyLogger
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Basic Usage
|
|
46
|
+
|
|
47
|
+
### Simple Logging
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from easy_logger import EasyLogger
|
|
51
|
+
|
|
52
|
+
# Create a logger with default settings
|
|
53
|
+
logger = EasyLogger()
|
|
54
|
+
|
|
55
|
+
# Log messages at different levels
|
|
56
|
+
logger.debug("This is a debug message")
|
|
57
|
+
logger.info("This is an info message")
|
|
58
|
+
logger.warning("This is a warning message")
|
|
59
|
+
logger.error("This is an error message")
|
|
60
|
+
logger.critical("This is a critical message")
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Advanced Configuration
|
|
64
|
+
|
|
65
|
+
### Custom Logger Name and Level
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
# Create a logger with custom name and log level
|
|
69
|
+
logger = EasyLogger(name="my_app", level="DEBUG")
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
**Available log levels:**
|
|
73
|
+
- `DEBUG` - Detailed information for debugging
|
|
74
|
+
- `INFO` - General informational messages (default)
|
|
75
|
+
- `WARNING` - Warning messages
|
|
76
|
+
- `ERROR` - Error messages
|
|
77
|
+
- `CRITICAL` - Critical error messages
|
|
78
|
+
|
|
79
|
+
### Custom Log Directory
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
from pathlib import Path
|
|
83
|
+
|
|
84
|
+
# Use a custom directory for log files
|
|
85
|
+
logger = EasyLogger(log_dir=Path("./custom_logs"))
|
|
86
|
+
|
|
87
|
+
# Or use a string path
|
|
88
|
+
logger = EasyLogger(log_dir="./app_logs")
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Disable Colored Output
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
# Disable colored console output
|
|
95
|
+
logger = EasyLogger(enable_colors=False)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Combined Configuration
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from pathlib import Path
|
|
102
|
+
|
|
103
|
+
# Combine multiple options
|
|
104
|
+
logger = EasyLogger(
|
|
105
|
+
name="my_app",
|
|
106
|
+
level="WARNING",
|
|
107
|
+
log_dir=Path("./app_logs"),
|
|
108
|
+
enable_colors=True
|
|
109
|
+
)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Logging Features
|
|
113
|
+
|
|
114
|
+
### String Formatting
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
# Use % formatting
|
|
118
|
+
logger.info("User %s logged in", username)
|
|
119
|
+
logger.error("Failed to process %s: %s", filename, error_msg)
|
|
120
|
+
|
|
121
|
+
# Multiple arguments
|
|
122
|
+
logger.debug("Processing item %d of %d", current, total)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Exception Logging
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
# Log exceptions with traceback
|
|
129
|
+
try:
|
|
130
|
+
risky_operation()
|
|
131
|
+
except Exception as e:
|
|
132
|
+
logger.error("Operation failed", exc_info=True)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Extra Context
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
# Add extra context to log messages
|
|
139
|
+
logger.info("User action", extra={'user_id': 123, 'action': 'login'})
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Features
|
|
143
|
+
|
|
144
|
+
### Automatic Features
|
|
145
|
+
|
|
146
|
+
- **Colored Console Output**: Automatically detects terminal color support
|
|
147
|
+
- DEBUG: Cyan
|
|
148
|
+
- INFO: Green
|
|
149
|
+
- WARNING: Yellow
|
|
150
|
+
- ERROR: Red
|
|
151
|
+
- CRITICAL: Purple
|
|
152
|
+
|
|
153
|
+
- **File Logging**: Automatically saves logs to files
|
|
154
|
+
- Default location: `./logs/YYYYMMDD_log.txt`
|
|
155
|
+
- Format: `20260206_log.txt`
|
|
156
|
+
- Encoding: UTF-8
|
|
157
|
+
|
|
158
|
+
- **Dual Output**: Logs are written to both console and file simultaneously
|
|
159
|
+
|
|
160
|
+
### Type Safety
|
|
161
|
+
|
|
162
|
+
The library includes full type hints for better IDE support and type checking:
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
from easy_logger import EasyLogger
|
|
166
|
+
from pathlib import Path
|
|
167
|
+
|
|
168
|
+
logger: EasyLogger = EasyLogger(
|
|
169
|
+
name="my_app",
|
|
170
|
+
level="INFO",
|
|
171
|
+
log_dir=Path("./logs"),
|
|
172
|
+
enable_colors=True
|
|
173
|
+
)
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Examples
|
|
177
|
+
|
|
178
|
+
### Web Application Logging
|
|
179
|
+
|
|
180
|
+
```python
|
|
181
|
+
from easy_logger import EasyLogger
|
|
182
|
+
|
|
183
|
+
logger = EasyLogger(name="web_app", level="INFO")
|
|
184
|
+
|
|
185
|
+
logger.info("Server started on port 8000")
|
|
186
|
+
logger.warning("High memory usage detected: 85%%")
|
|
187
|
+
logger.error("Database connection failed", exc_info=True)
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### Script Logging
|
|
191
|
+
|
|
192
|
+
```python
|
|
193
|
+
from easy_logger import EasyLogger
|
|
194
|
+
from pathlib import Path
|
|
195
|
+
|
|
196
|
+
logger = EasyLogger(
|
|
197
|
+
name="data_processor",
|
|
198
|
+
level="DEBUG",
|
|
199
|
+
log_dir=Path("./script_logs")
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
logger.debug("Starting data processing")
|
|
203
|
+
logger.info("Processed %d records", record_count)
|
|
204
|
+
logger.warning("Skipped %d invalid records", invalid_count)
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Production Logging
|
|
208
|
+
|
|
209
|
+
```python
|
|
210
|
+
from easy_logger import EasyLogger
|
|
211
|
+
|
|
212
|
+
# Production: WARNING level, no colors for log aggregation
|
|
213
|
+
logger = EasyLogger(
|
|
214
|
+
name="production_app",
|
|
215
|
+
level="WARNING",
|
|
216
|
+
enable_colors=False
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
logger.warning("API rate limit approaching")
|
|
220
|
+
logger.error("Payment processing failed for order %s", order_id)
|
|
221
|
+
logger.critical("Database connection pool exhausted")
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
## Log File Management
|
|
225
|
+
|
|
226
|
+
Log files are automatically created with the current date:
|
|
227
|
+
- Format: `YYYYMMDD_log.txt`
|
|
228
|
+
- Example: `20260206_log.txt`
|
|
229
|
+
- Location: `./logs/` (or custom directory)
|
|
230
|
+
|
|
231
|
+
Each day creates a new log file, making it easy to manage and archive logs.
|
|
232
|
+
|
|
233
|
+
## Notes
|
|
234
|
+
|
|
235
|
+
- The logger automatically creates the log directory if it doesn't exist
|
|
236
|
+
- Console colors are automatically disabled when output is redirected or piped
|
|
237
|
+
- File logs never contain color codes, only plain text
|
|
238
|
+
- Multiple logger instances can coexist with different configurations
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# EasyLogger Usage Guide
|
|
2
|
+
|
|
3
|
+
A simplified Python logging library with colored console output and automatic file logging.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```python
|
|
8
|
+
from easy_logger import EasyLogger
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Basic Usage
|
|
12
|
+
|
|
13
|
+
### Simple Logging
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from easy_logger import EasyLogger
|
|
17
|
+
|
|
18
|
+
# Create a logger with default settings
|
|
19
|
+
logger = EasyLogger()
|
|
20
|
+
|
|
21
|
+
# Log messages at different levels
|
|
22
|
+
logger.debug("This is a debug message")
|
|
23
|
+
logger.info("This is an info message")
|
|
24
|
+
logger.warning("This is a warning message")
|
|
25
|
+
logger.error("This is an error message")
|
|
26
|
+
logger.critical("This is a critical message")
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Advanced Configuration
|
|
30
|
+
|
|
31
|
+
### Custom Logger Name and Level
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
# Create a logger with custom name and log level
|
|
35
|
+
logger = EasyLogger(name="my_app", level="DEBUG")
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
**Available log levels:**
|
|
39
|
+
- `DEBUG` - Detailed information for debugging
|
|
40
|
+
- `INFO` - General informational messages (default)
|
|
41
|
+
- `WARNING` - Warning messages
|
|
42
|
+
- `ERROR` - Error messages
|
|
43
|
+
- `CRITICAL` - Critical error messages
|
|
44
|
+
|
|
45
|
+
### Custom Log Directory
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
from pathlib import Path
|
|
49
|
+
|
|
50
|
+
# Use a custom directory for log files
|
|
51
|
+
logger = EasyLogger(log_dir=Path("./custom_logs"))
|
|
52
|
+
|
|
53
|
+
# Or use a string path
|
|
54
|
+
logger = EasyLogger(log_dir="./app_logs")
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Disable Colored Output
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
# Disable colored console output
|
|
61
|
+
logger = EasyLogger(enable_colors=False)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Combined Configuration
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
from pathlib import Path
|
|
68
|
+
|
|
69
|
+
# Combine multiple options
|
|
70
|
+
logger = EasyLogger(
|
|
71
|
+
name="my_app",
|
|
72
|
+
level="WARNING",
|
|
73
|
+
log_dir=Path("./app_logs"),
|
|
74
|
+
enable_colors=True
|
|
75
|
+
)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Logging Features
|
|
79
|
+
|
|
80
|
+
### String Formatting
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
# Use % formatting
|
|
84
|
+
logger.info("User %s logged in", username)
|
|
85
|
+
logger.error("Failed to process %s: %s", filename, error_msg)
|
|
86
|
+
|
|
87
|
+
# Multiple arguments
|
|
88
|
+
logger.debug("Processing item %d of %d", current, total)
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Exception Logging
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
# Log exceptions with traceback
|
|
95
|
+
try:
|
|
96
|
+
risky_operation()
|
|
97
|
+
except Exception as e:
|
|
98
|
+
logger.error("Operation failed", exc_info=True)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Extra Context
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
# Add extra context to log messages
|
|
105
|
+
logger.info("User action", extra={'user_id': 123, 'action': 'login'})
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Features
|
|
109
|
+
|
|
110
|
+
### Automatic Features
|
|
111
|
+
|
|
112
|
+
- **Colored Console Output**: Automatically detects terminal color support
|
|
113
|
+
- DEBUG: Cyan
|
|
114
|
+
- INFO: Green
|
|
115
|
+
- WARNING: Yellow
|
|
116
|
+
- ERROR: Red
|
|
117
|
+
- CRITICAL: Purple
|
|
118
|
+
|
|
119
|
+
- **File Logging**: Automatically saves logs to files
|
|
120
|
+
- Default location: `./logs/YYYYMMDD_log.txt`
|
|
121
|
+
- Format: `20260206_log.txt`
|
|
122
|
+
- Encoding: UTF-8
|
|
123
|
+
|
|
124
|
+
- **Dual Output**: Logs are written to both console and file simultaneously
|
|
125
|
+
|
|
126
|
+
### Type Safety
|
|
127
|
+
|
|
128
|
+
The library includes full type hints for better IDE support and type checking:
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
from easy_logger import EasyLogger
|
|
132
|
+
from pathlib import Path
|
|
133
|
+
|
|
134
|
+
logger: EasyLogger = EasyLogger(
|
|
135
|
+
name="my_app",
|
|
136
|
+
level="INFO",
|
|
137
|
+
log_dir=Path("./logs"),
|
|
138
|
+
enable_colors=True
|
|
139
|
+
)
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Examples
|
|
143
|
+
|
|
144
|
+
### Web Application Logging
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
from easy_logger import EasyLogger
|
|
148
|
+
|
|
149
|
+
logger = EasyLogger(name="web_app", level="INFO")
|
|
150
|
+
|
|
151
|
+
logger.info("Server started on port 8000")
|
|
152
|
+
logger.warning("High memory usage detected: 85%%")
|
|
153
|
+
logger.error("Database connection failed", exc_info=True)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Script Logging
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
from easy_logger import EasyLogger
|
|
160
|
+
from pathlib import Path
|
|
161
|
+
|
|
162
|
+
logger = EasyLogger(
|
|
163
|
+
name="data_processor",
|
|
164
|
+
level="DEBUG",
|
|
165
|
+
log_dir=Path("./script_logs")
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
logger.debug("Starting data processing")
|
|
169
|
+
logger.info("Processed %d records", record_count)
|
|
170
|
+
logger.warning("Skipped %d invalid records", invalid_count)
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Production Logging
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
from easy_logger import EasyLogger
|
|
177
|
+
|
|
178
|
+
# Production: WARNING level, no colors for log aggregation
|
|
179
|
+
logger = EasyLogger(
|
|
180
|
+
name="production_app",
|
|
181
|
+
level="WARNING",
|
|
182
|
+
enable_colors=False
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
logger.warning("API rate limit approaching")
|
|
186
|
+
logger.error("Payment processing failed for order %s", order_id)
|
|
187
|
+
logger.critical("Database connection pool exhausted")
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## Log File Management
|
|
191
|
+
|
|
192
|
+
Log files are automatically created with the current date:
|
|
193
|
+
- Format: `YYYYMMDD_log.txt`
|
|
194
|
+
- Example: `20260206_log.txt`
|
|
195
|
+
- Location: `./logs/` (or custom directory)
|
|
196
|
+
|
|
197
|
+
Each day creates a new log file, making it easy to manage and archive logs.
|
|
198
|
+
|
|
199
|
+
## Notes
|
|
200
|
+
|
|
201
|
+
- The logger automatically creates the log directory if it doesn't exist
|
|
202
|
+
- Console colors are automatically disabled when output is redirected or piped
|
|
203
|
+
- File logs never contain color codes, only plain text
|
|
204
|
+
- Multiple logger instances can coexist with different configurations
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# easy_logger/__init__.py
|
|
2
|
+
"""
|
|
3
|
+
EasyLogger - A simplified Python logging library with colored console output.
|
|
4
|
+
|
|
5
|
+
This package provides an easy-to-use logging interface with automatic
|
|
6
|
+
file logging and colored console output.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from .core import EasyLogger
|
|
10
|
+
|
|
11
|
+
__version__ = "0.2.0"
|
|
12
|
+
__author__ = "aiwonderland"
|
|
13
|
+
__all__ = [
|
|
14
|
+
"EasyLogger"
|
|
15
|
+
]
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# easy_logger/core.py
|
|
2
|
+
import logging
|
|
3
|
+
import sys
|
|
4
|
+
from logging.handlers import RotatingFileHandler
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Any, Optional
|
|
7
|
+
from .utils import _get_log_filename, _ensure_log_dir
|
|
8
|
+
|
|
9
|
+
# Define colored output format (make logs more intuitive)
|
|
10
|
+
COLORS = {
|
|
11
|
+
"DEBUG": "\033[0;36m", # Cyan
|
|
12
|
+
"INFO": "\033[0;32m", # Green
|
|
13
|
+
"WARNING": "\033[0;33m", # Yellow
|
|
14
|
+
"ERROR": "\033[0;31m", # Red
|
|
15
|
+
"CRITICAL": "\033[0;35m" # Purple
|
|
16
|
+
}
|
|
17
|
+
RESET = "\033[0m"
|
|
18
|
+
|
|
19
|
+
def _supports_color() -> bool:
|
|
20
|
+
"""Check if the terminal supports color output."""
|
|
21
|
+
return hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
|
|
22
|
+
|
|
23
|
+
class EasyLogger:
|
|
24
|
+
def __init__(
|
|
25
|
+
self,
|
|
26
|
+
name: str = "easy_logger",
|
|
27
|
+
level: str = "INFO",
|
|
28
|
+
log_dir: Optional[Path] = None,
|
|
29
|
+
enable_colors: bool = True,
|
|
30
|
+
max_bytes: int = 10 * 1024 * 1024, # 10MB default
|
|
31
|
+
backup_count: int = 5 # Keep 5 backup files
|
|
32
|
+
):
|
|
33
|
+
"""
|
|
34
|
+
Initialize a simplified logger with console and file output.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
name: Logger name
|
|
38
|
+
level: Log level (DEBUG/INFO/WARNING/ERROR/CRITICAL)
|
|
39
|
+
log_dir: Custom log directory (default: uses ensure_log_dir())
|
|
40
|
+
enable_colors: Enable colored console output (default: True)
|
|
41
|
+
max_bytes: Maximum size of each log file before rotation (default: 10MB)
|
|
42
|
+
backup_count: Number of backup log files to keep (default: 5)
|
|
43
|
+
"""
|
|
44
|
+
# 1. Create logger instance
|
|
45
|
+
self.logger = logging.getLogger(name)
|
|
46
|
+
self.logger.setLevel(getattr(logging, level.upper()))
|
|
47
|
+
self.logger.handlers.clear() # Avoid duplicate handlers
|
|
48
|
+
self.enable_colors = enable_colors and _supports_color()
|
|
49
|
+
|
|
50
|
+
# 2. Define log format
|
|
51
|
+
formatter = logging.Formatter(
|
|
52
|
+
"%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
|
53
|
+
datefmt="%Y-%m-%d %H:%M:%S"
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
# 3. Console handler (colored output)
|
|
57
|
+
console_handler = logging.StreamHandler()
|
|
58
|
+
if self.enable_colors:
|
|
59
|
+
console_handler.setFormatter(self._get_color_formatter(formatter))
|
|
60
|
+
else:
|
|
61
|
+
console_handler.setFormatter(formatter)
|
|
62
|
+
self.logger.addHandler(console_handler)
|
|
63
|
+
|
|
64
|
+
# 4. File handler (save to log file with rotation)
|
|
65
|
+
log_directory = _ensure_log_dir(log_dir) if log_dir else _ensure_log_dir()
|
|
66
|
+
log_file = log_directory / _get_log_filename()
|
|
67
|
+
file_handler = RotatingFileHandler(
|
|
68
|
+
log_file,
|
|
69
|
+
maxBytes=max_bytes,
|
|
70
|
+
backupCount=backup_count,
|
|
71
|
+
encoding="utf-8"
|
|
72
|
+
)
|
|
73
|
+
file_handler.setFormatter(formatter)
|
|
74
|
+
self.logger.addHandler(file_handler)
|
|
75
|
+
|
|
76
|
+
def _get_color_formatter(self, base_formatter: logging.Formatter) -> logging.Formatter:
|
|
77
|
+
"""
|
|
78
|
+
Create a formatter that adds color codes to log output.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
base_formatter: The base formatter to wrap
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
A ColorFormatter instance
|
|
85
|
+
"""
|
|
86
|
+
class ColorFormatter(logging.Formatter):
|
|
87
|
+
def format(self, record: logging.LogRecord) -> str:
|
|
88
|
+
# Store original values
|
|
89
|
+
original_levelname = record.levelname
|
|
90
|
+
|
|
91
|
+
# Add color to different log levels
|
|
92
|
+
if record.levelname in COLORS:
|
|
93
|
+
color = COLORS[record.levelname]
|
|
94
|
+
record.levelname = f"{color}{record.levelname}{RESET}"
|
|
95
|
+
|
|
96
|
+
# Format the message
|
|
97
|
+
result = base_formatter.format(record)
|
|
98
|
+
|
|
99
|
+
# Restore original levelname
|
|
100
|
+
record.levelname = original_levelname
|
|
101
|
+
|
|
102
|
+
return result
|
|
103
|
+
return ColorFormatter()
|
|
104
|
+
|
|
105
|
+
# Expose logging methods (simplify calls)
|
|
106
|
+
def debug(self, msg: Any, *args: Any, **kwargs: Any) -> None:
|
|
107
|
+
"""Log a debug message."""
|
|
108
|
+
self.logger.debug(msg, *args, **kwargs)
|
|
109
|
+
|
|
110
|
+
def info(self, msg: Any, *args: Any, **kwargs: Any) -> None:
|
|
111
|
+
"""Log an info message."""
|
|
112
|
+
self.logger.info(msg, *args, **kwargs)
|
|
113
|
+
|
|
114
|
+
def warning(self, msg: Any, *args: Any, **kwargs: Any) -> None:
|
|
115
|
+
"""Log a warning message."""
|
|
116
|
+
self.logger.warning(msg, *args, **kwargs)
|
|
117
|
+
|
|
118
|
+
def error(self, msg: Any, *args: Any, **kwargs: Any) -> None:
|
|
119
|
+
"""Log an error message."""
|
|
120
|
+
self.logger.error(msg, *args, **kwargs)
|
|
121
|
+
|
|
122
|
+
def critical(self, msg: Any, *args: Any, **kwargs: Any) -> None:
|
|
123
|
+
"""Log a critical message."""
|
|
124
|
+
self.logger.critical(msg, *args, **kwargs)
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# easy_logger/utils.py
|
|
2
|
+
from datetime import datetime
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Union
|
|
5
|
+
|
|
6
|
+
DEFAULT_LOG_DIR = Path("./logs")
|
|
7
|
+
|
|
8
|
+
def _get_log_filename(date_format: str = "%Y%m%d") -> str:
|
|
9
|
+
"""
|
|
10
|
+
Generate a log filename with a date.
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
date_format: The date format string (default: "%Y%m%d")
|
|
14
|
+
|
|
15
|
+
Returns:
|
|
16
|
+
A filename string like "20260206_log.txt"
|
|
17
|
+
"""
|
|
18
|
+
today = datetime.now().strftime(date_format)
|
|
19
|
+
return f"{today}_log.txt"
|
|
20
|
+
|
|
21
|
+
def _ensure_log_dir(log_dir: Union[str, Path] = DEFAULT_LOG_DIR) -> Path:
|
|
22
|
+
"""
|
|
23
|
+
Ensure the log directory exists; create it if it does not.
|
|
24
|
+
|
|
25
|
+
Args:
|
|
26
|
+
log_dir: The directory path (default: "./logs")
|
|
27
|
+
|
|
28
|
+
Returns:
|
|
29
|
+
The Path object for the log directory
|
|
30
|
+
|
|
31
|
+
Raises:
|
|
32
|
+
OSError: If directory creation fails
|
|
33
|
+
"""
|
|
34
|
+
log_path = Path(log_dir)
|
|
35
|
+
log_path.mkdir(parents=True, exist_ok=True)
|
|
36
|
+
return log_path
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: easy-logy
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A simplified Python logging library with colored console output and automatic file logging
|
|
5
|
+
Author-email: aiwonderland <quantbit@126.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/aiwonderland/easy_logger
|
|
7
|
+
Project-URL: Repository, https://github.com/aiwonderland/easy_logger
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/aiwonderland/easy_logger/issues
|
|
9
|
+
Project-URL: Documentation, https://github.com/aiwonderland/easy_logger#readme
|
|
10
|
+
Keywords: logging,logger,colored-output,file-logging,easy-logging,python-logging
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
22
|
+
Classifier: Topic :: System :: Logging
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.8
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
License-File: LICENSE.md
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
29
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
30
|
+
Requires-Dist: mypy>=1.0.0; extra == "dev"
|
|
31
|
+
Requires-Dist: black>=23.0.0; extra == "dev"
|
|
32
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
|
|
35
|
+
# EasyLogger Usage Guide
|
|
36
|
+
|
|
37
|
+
A simplified Python logging library with colored console output and automatic file logging.
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from easy_logger import EasyLogger
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Basic Usage
|
|
46
|
+
|
|
47
|
+
### Simple Logging
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from easy_logger import EasyLogger
|
|
51
|
+
|
|
52
|
+
# Create a logger with default settings
|
|
53
|
+
logger = EasyLogger()
|
|
54
|
+
|
|
55
|
+
# Log messages at different levels
|
|
56
|
+
logger.debug("This is a debug message")
|
|
57
|
+
logger.info("This is an info message")
|
|
58
|
+
logger.warning("This is a warning message")
|
|
59
|
+
logger.error("This is an error message")
|
|
60
|
+
logger.critical("This is a critical message")
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Advanced Configuration
|
|
64
|
+
|
|
65
|
+
### Custom Logger Name and Level
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
# Create a logger with custom name and log level
|
|
69
|
+
logger = EasyLogger(name="my_app", level="DEBUG")
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
**Available log levels:**
|
|
73
|
+
- `DEBUG` - Detailed information for debugging
|
|
74
|
+
- `INFO` - General informational messages (default)
|
|
75
|
+
- `WARNING` - Warning messages
|
|
76
|
+
- `ERROR` - Error messages
|
|
77
|
+
- `CRITICAL` - Critical error messages
|
|
78
|
+
|
|
79
|
+
### Custom Log Directory
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
from pathlib import Path
|
|
83
|
+
|
|
84
|
+
# Use a custom directory for log files
|
|
85
|
+
logger = EasyLogger(log_dir=Path("./custom_logs"))
|
|
86
|
+
|
|
87
|
+
# Or use a string path
|
|
88
|
+
logger = EasyLogger(log_dir="./app_logs")
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Disable Colored Output
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
# Disable colored console output
|
|
95
|
+
logger = EasyLogger(enable_colors=False)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Combined Configuration
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from pathlib import Path
|
|
102
|
+
|
|
103
|
+
# Combine multiple options
|
|
104
|
+
logger = EasyLogger(
|
|
105
|
+
name="my_app",
|
|
106
|
+
level="WARNING",
|
|
107
|
+
log_dir=Path("./app_logs"),
|
|
108
|
+
enable_colors=True
|
|
109
|
+
)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Logging Features
|
|
113
|
+
|
|
114
|
+
### String Formatting
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
# Use % formatting
|
|
118
|
+
logger.info("User %s logged in", username)
|
|
119
|
+
logger.error("Failed to process %s: %s", filename, error_msg)
|
|
120
|
+
|
|
121
|
+
# Multiple arguments
|
|
122
|
+
logger.debug("Processing item %d of %d", current, total)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Exception Logging
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
# Log exceptions with traceback
|
|
129
|
+
try:
|
|
130
|
+
risky_operation()
|
|
131
|
+
except Exception as e:
|
|
132
|
+
logger.error("Operation failed", exc_info=True)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Extra Context
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
# Add extra context to log messages
|
|
139
|
+
logger.info("User action", extra={'user_id': 123, 'action': 'login'})
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Features
|
|
143
|
+
|
|
144
|
+
### Automatic Features
|
|
145
|
+
|
|
146
|
+
- **Colored Console Output**: Automatically detects terminal color support
|
|
147
|
+
- DEBUG: Cyan
|
|
148
|
+
- INFO: Green
|
|
149
|
+
- WARNING: Yellow
|
|
150
|
+
- ERROR: Red
|
|
151
|
+
- CRITICAL: Purple
|
|
152
|
+
|
|
153
|
+
- **File Logging**: Automatically saves logs to files
|
|
154
|
+
- Default location: `./logs/YYYYMMDD_log.txt`
|
|
155
|
+
- Format: `20260206_log.txt`
|
|
156
|
+
- Encoding: UTF-8
|
|
157
|
+
|
|
158
|
+
- **Dual Output**: Logs are written to both console and file simultaneously
|
|
159
|
+
|
|
160
|
+
### Type Safety
|
|
161
|
+
|
|
162
|
+
The library includes full type hints for better IDE support and type checking:
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
from easy_logger import EasyLogger
|
|
166
|
+
from pathlib import Path
|
|
167
|
+
|
|
168
|
+
logger: EasyLogger = EasyLogger(
|
|
169
|
+
name="my_app",
|
|
170
|
+
level="INFO",
|
|
171
|
+
log_dir=Path("./logs"),
|
|
172
|
+
enable_colors=True
|
|
173
|
+
)
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Examples
|
|
177
|
+
|
|
178
|
+
### Web Application Logging
|
|
179
|
+
|
|
180
|
+
```python
|
|
181
|
+
from easy_logger import EasyLogger
|
|
182
|
+
|
|
183
|
+
logger = EasyLogger(name="web_app", level="INFO")
|
|
184
|
+
|
|
185
|
+
logger.info("Server started on port 8000")
|
|
186
|
+
logger.warning("High memory usage detected: 85%%")
|
|
187
|
+
logger.error("Database connection failed", exc_info=True)
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### Script Logging
|
|
191
|
+
|
|
192
|
+
```python
|
|
193
|
+
from easy_logger import EasyLogger
|
|
194
|
+
from pathlib import Path
|
|
195
|
+
|
|
196
|
+
logger = EasyLogger(
|
|
197
|
+
name="data_processor",
|
|
198
|
+
level="DEBUG",
|
|
199
|
+
log_dir=Path("./script_logs")
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
logger.debug("Starting data processing")
|
|
203
|
+
logger.info("Processed %d records", record_count)
|
|
204
|
+
logger.warning("Skipped %d invalid records", invalid_count)
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Production Logging
|
|
208
|
+
|
|
209
|
+
```python
|
|
210
|
+
from easy_logger import EasyLogger
|
|
211
|
+
|
|
212
|
+
# Production: WARNING level, no colors for log aggregation
|
|
213
|
+
logger = EasyLogger(
|
|
214
|
+
name="production_app",
|
|
215
|
+
level="WARNING",
|
|
216
|
+
enable_colors=False
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
logger.warning("API rate limit approaching")
|
|
220
|
+
logger.error("Payment processing failed for order %s", order_id)
|
|
221
|
+
logger.critical("Database connection pool exhausted")
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
## Log File Management
|
|
225
|
+
|
|
226
|
+
Log files are automatically created with the current date:
|
|
227
|
+
- Format: `YYYYMMDD_log.txt`
|
|
228
|
+
- Example: `20260206_log.txt`
|
|
229
|
+
- Location: `./logs/` (or custom directory)
|
|
230
|
+
|
|
231
|
+
Each day creates a new log file, making it easy to manage and archive logs.
|
|
232
|
+
|
|
233
|
+
## Notes
|
|
234
|
+
|
|
235
|
+
- The logger automatically creates the log directory if it doesn't exist
|
|
236
|
+
- Console colors are automatically disabled when output is redirected or piped
|
|
237
|
+
- File logs never contain color codes, only plain text
|
|
238
|
+
- Multiple logger instances can coexist with different configurations
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
LICENSE.md
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
easy_logger/__init__.py
|
|
5
|
+
easy_logger/core.py
|
|
6
|
+
easy_logger/utils.py
|
|
7
|
+
easy_logy.egg-info/PKG-INFO
|
|
8
|
+
easy_logy.egg-info/SOURCES.txt
|
|
9
|
+
easy_logy.egg-info/dependency_links.txt
|
|
10
|
+
easy_logy.egg-info/requires.txt
|
|
11
|
+
easy_logy.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
easy_logger
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "easy-logy"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A simplified Python logging library with colored console output and automatic file logging"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { file = "LICENSE" }
|
|
11
|
+
authors = [
|
|
12
|
+
{ name = "aiwonderland", email = "quantbit@126.com" }
|
|
13
|
+
]
|
|
14
|
+
keywords = [
|
|
15
|
+
"logging",
|
|
16
|
+
"logger",
|
|
17
|
+
"colored-output",
|
|
18
|
+
"file-logging",
|
|
19
|
+
"easy-logging",
|
|
20
|
+
"python-logging"
|
|
21
|
+
]
|
|
22
|
+
classifiers = [
|
|
23
|
+
"Development Status :: 4 - Beta",
|
|
24
|
+
"Intended Audience :: Developers",
|
|
25
|
+
"License :: OSI Approved :: MIT License",
|
|
26
|
+
"Operating System :: OS Independent",
|
|
27
|
+
"Programming Language :: Python :: 3",
|
|
28
|
+
"Programming Language :: Python :: 3.8",
|
|
29
|
+
"Programming Language :: Python :: 3.9",
|
|
30
|
+
"Programming Language :: Python :: 3.10",
|
|
31
|
+
"Programming Language :: Python :: 3.11",
|
|
32
|
+
"Programming Language :: Python :: 3.12",
|
|
33
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
34
|
+
"Topic :: System :: Logging",
|
|
35
|
+
"Typing :: Typed"
|
|
36
|
+
]
|
|
37
|
+
requires-python = ">=3.8"
|
|
38
|
+
dependencies = []
|
|
39
|
+
|
|
40
|
+
[project.optional-dependencies]
|
|
41
|
+
dev = [
|
|
42
|
+
"pytest>=7.0.0",
|
|
43
|
+
"pytest-cov>=4.0.0",
|
|
44
|
+
"mypy>=1.0.0",
|
|
45
|
+
"black>=23.0.0",
|
|
46
|
+
"ruff>=0.1.0"
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
[project.urls]
|
|
50
|
+
Homepage = "https://github.com/aiwonderland/easy_logger"
|
|
51
|
+
Repository = "https://github.com/aiwonderland/easy_logger"
|
|
52
|
+
"Bug Tracker" = "https://github.com/aiwonderland/easy_logger/issues"
|
|
53
|
+
Documentation = "https://github.com/aiwonderland/easy_logger#readme"
|
|
54
|
+
|
|
55
|
+
[tool.setuptools.packages.find]
|
|
56
|
+
where = ["."]
|
|
57
|
+
include = ["easy_logger*"]
|
|
58
|
+
exclude = ["tests*"]
|