py3-logger 0.0.1__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.
- py3_logger-0.0.1/LICENSE +21 -0
- py3_logger-0.0.1/PKG-INFO +20 -0
- py3_logger-0.0.1/README.md +2 -0
- py3_logger-0.0.1/pyproject.toml +18 -0
- py3_logger-0.0.1/src/logger.py +229 -0
py3_logger-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 mathewgeola
|
|
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.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: py3-logger
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: python3 logger
|
|
5
|
+
License: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Author: mathewgeola
|
|
8
|
+
Author-email: mathewgeola@gmail.com
|
|
9
|
+
Requires-Python: >=3.12
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Requires-Dist: colorlog (>=6.10.1,<7.0.0)
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
# py3-logger
|
|
19
|
+
python3 logger
|
|
20
|
+
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "py3-logger"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
description = "python3 logger"
|
|
5
|
+
authors = ["mathewgeola <mathewgeola@gmail.com>"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
packages = [
|
|
9
|
+
{ include = "logger.py", from = "src" }
|
|
10
|
+
]
|
|
11
|
+
|
|
12
|
+
[tool.poetry.dependencies]
|
|
13
|
+
python = ">=3.12"
|
|
14
|
+
colorlog = "^6.10.1"
|
|
15
|
+
|
|
16
|
+
[build-system]
|
|
17
|
+
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
|
18
|
+
build-backend = "poetry.core.masonry.api"
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import os
|
|
5
|
+
import sys
|
|
6
|
+
from logging import handlers
|
|
7
|
+
from typing import Any, Literal, Final, cast
|
|
8
|
+
|
|
9
|
+
import colorlog
|
|
10
|
+
|
|
11
|
+
FRAMEWORK: Final[int] = 55
|
|
12
|
+
SUCCESS: Final[int] = 25
|
|
13
|
+
|
|
14
|
+
logging.addLevelName(FRAMEWORK, "FRAMEWORK")
|
|
15
|
+
logging.addLevelName(SUCCESS, "SUCCESS")
|
|
16
|
+
|
|
17
|
+
LoggerLevel = Literal["DEBUG", "INFO", "SUCCESS", "WARNING", "ERROR", "CRITICAL", "FRAMEWORK"]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class Logger(logging.Logger):
|
|
21
|
+
def framework(
|
|
22
|
+
self,
|
|
23
|
+
msg: str,
|
|
24
|
+
*args: Any,
|
|
25
|
+
level: LoggerLevel = "DEBUG",
|
|
26
|
+
**kwargs: Any
|
|
27
|
+
) -> None:
|
|
28
|
+
if self.isEnabledFor(FRAMEWORK):
|
|
29
|
+
msg = f"[{level}] {msg}"
|
|
30
|
+
kwargs.setdefault("stacklevel", 2)
|
|
31
|
+
self._log(FRAMEWORK, msg, args, **kwargs)
|
|
32
|
+
|
|
33
|
+
def success(self, msg: str, *args: Any, **kwargs: Any) -> None:
|
|
34
|
+
if self.isEnabledFor(SUCCESS):
|
|
35
|
+
kwargs.setdefault("stacklevel", 2)
|
|
36
|
+
self._log(SUCCESS, msg, args, **kwargs)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
logging.setLoggerClass(Logger)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class RotatingFileHandler(handlers.RotatingFileHandler):
|
|
43
|
+
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
44
|
+
super().__init__(*args, **kwargs)
|
|
45
|
+
self._zero_padding: Final[int] = len(str(self.backupCount)) if self.backupCount > 0 else 0
|
|
46
|
+
|
|
47
|
+
def doRollover(self) -> None:
|
|
48
|
+
if self.stream:
|
|
49
|
+
self.stream.close()
|
|
50
|
+
self.stream = None # noqa
|
|
51
|
+
|
|
52
|
+
if self.backupCount > 0:
|
|
53
|
+
oldest = self._format_backup_filename(self.backupCount)
|
|
54
|
+
if os.path.exists(oldest):
|
|
55
|
+
os.remove(oldest)
|
|
56
|
+
|
|
57
|
+
for i in range(self.backupCount - 1, 0, -1):
|
|
58
|
+
sfn = self._format_backup_filename(i)
|
|
59
|
+
dfn = self._format_backup_filename(i + 1)
|
|
60
|
+
if os.path.exists(sfn):
|
|
61
|
+
os.rename(sfn, dfn)
|
|
62
|
+
|
|
63
|
+
dfn = self._format_backup_filename(1)
|
|
64
|
+
if os.path.exists(self.baseFilename):
|
|
65
|
+
os.rename(self.baseFilename, dfn)
|
|
66
|
+
|
|
67
|
+
self.mode = "w"
|
|
68
|
+
self.stream = self._open()
|
|
69
|
+
|
|
70
|
+
def _format_backup_filename(self, index: int) -> str:
|
|
71
|
+
root, ext = os.path.splitext(self.baseFilename)
|
|
72
|
+
padded = str(index).zfill(self._zero_padding)
|
|
73
|
+
return f"{root}.{padded}{ext}"
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _set_console_handler(
|
|
77
|
+
_logger: Logger,
|
|
78
|
+
console_level: str | int, console_fmt: str
|
|
79
|
+
) -> Logger:
|
|
80
|
+
console_handler = logging.StreamHandler()
|
|
81
|
+
console_handler.setLevel(console_level)
|
|
82
|
+
green = {
|
|
83
|
+
"DEBUG": "green",
|
|
84
|
+
"INFO": "green",
|
|
85
|
+
"SUCCESS": "green",
|
|
86
|
+
"WARNING": "green",
|
|
87
|
+
"ERROR": "green",
|
|
88
|
+
"CRITICAL": "green",
|
|
89
|
+
"FRAMEWORK": "green",
|
|
90
|
+
}
|
|
91
|
+
bold_cyan = {
|
|
92
|
+
"DEBUG": "bold_cyan",
|
|
93
|
+
"INFO": "bold_cyan",
|
|
94
|
+
"SUCCESS": "bold_cyan",
|
|
95
|
+
"WARNING": "bold_cyan",
|
|
96
|
+
"ERROR": "bold_cyan",
|
|
97
|
+
"CRITICAL": "bold_cyan",
|
|
98
|
+
"FRAMEWORK": "bold_cyan",
|
|
99
|
+
}
|
|
100
|
+
log_colors = {
|
|
101
|
+
"DEBUG": "light_blue",
|
|
102
|
+
"INFO": "light_white",
|
|
103
|
+
"SUCCESS": "light_green",
|
|
104
|
+
"WARNING": "light_yellow",
|
|
105
|
+
"ERROR": "light_red",
|
|
106
|
+
"CRITICAL": "bg_red,light_white",
|
|
107
|
+
"FRAMEWORK": "light_red,bold",
|
|
108
|
+
}
|
|
109
|
+
secondary_log_colors = dict(
|
|
110
|
+
asctime=green,
|
|
111
|
+
name=bold_cyan,
|
|
112
|
+
levelname=log_colors,
|
|
113
|
+
process=bold_cyan,
|
|
114
|
+
processName=bold_cyan,
|
|
115
|
+
thread=bold_cyan,
|
|
116
|
+
threadName=bold_cyan,
|
|
117
|
+
pathname=bold_cyan,
|
|
118
|
+
funcName=bold_cyan,
|
|
119
|
+
lineno=bold_cyan,
|
|
120
|
+
message=log_colors,
|
|
121
|
+
)
|
|
122
|
+
console_formatter = colorlog.ColoredFormatter(
|
|
123
|
+
console_fmt,
|
|
124
|
+
secondary_log_colors=secondary_log_colors,
|
|
125
|
+
)
|
|
126
|
+
console_handler.setFormatter(console_formatter)
|
|
127
|
+
_logger.addHandler(console_handler)
|
|
128
|
+
return _logger
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _set_file_handler(
|
|
132
|
+
_logger: Logger,
|
|
133
|
+
file_level: str | int, file_fmt: str,
|
|
134
|
+
file_path: str, file_mode: str, file_max_bytes: int, file_backup_count: int, file_encoding: str,
|
|
135
|
+
) -> Logger:
|
|
136
|
+
file_handler = RotatingFileHandler(
|
|
137
|
+
file_path,
|
|
138
|
+
mode=file_mode, maxBytes=file_max_bytes, backupCount=file_backup_count, encoding=file_encoding
|
|
139
|
+
)
|
|
140
|
+
file_handler.setLevel(file_level)
|
|
141
|
+
file_formatter = logging.Formatter(file_fmt)
|
|
142
|
+
file_handler.setFormatter(file_formatter)
|
|
143
|
+
_logger.addHandler(file_handler)
|
|
144
|
+
return _logger
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def get_logger(
|
|
148
|
+
name: str | None = None,
|
|
149
|
+
level: str = "DEBUG",
|
|
150
|
+
to_console: bool = True,
|
|
151
|
+
console_level: str = "DEBUG",
|
|
152
|
+
console_fmt: str = (
|
|
153
|
+
"%(asctime_log_color)s%(asctime)s %(reset)s| "
|
|
154
|
+
"%(name_log_color)s%(name)s %(reset)s| "
|
|
155
|
+
"%(levelname_log_color)s%(levelname)s %(reset)s| "
|
|
156
|
+
# "%(process_log_color)sProcess: %(process)d %(processName_log_color)s(%(processName)s) %(reset)s| "
|
|
157
|
+
# "%(thread_log_color)sThread: %(thread)d %(threadName_log_color)s(%(threadName)s) %(reset)s| "
|
|
158
|
+
# "%(pathname_log_color)s%(pathname)s %(reset)s| "
|
|
159
|
+
"%(funcName_log_color)s%(funcName)s:%(lineno)d %(reset)s- "
|
|
160
|
+
"%(message_log_color)s%(message)s"
|
|
161
|
+
),
|
|
162
|
+
to_file: bool = False,
|
|
163
|
+
file_level: str = "DEBUG",
|
|
164
|
+
file_path: str | None = None,
|
|
165
|
+
file_mode: str = "a",
|
|
166
|
+
file_max_bytes: int = 10 * 1024 * 1024,
|
|
167
|
+
file_backup_count: int = 20,
|
|
168
|
+
file_encoding: str = "utf8",
|
|
169
|
+
file_fmt: str = (
|
|
170
|
+
"%(asctime)s | "
|
|
171
|
+
"%(name)s | "
|
|
172
|
+
"%(levelname)s | "
|
|
173
|
+
# "Process: %(process)d (%(processName)s) | "
|
|
174
|
+
# "Thread: %(thread)d (%(threadName)s) | "
|
|
175
|
+
# "%(pathname)s | "
|
|
176
|
+
"%(funcName)s:%(lineno)d - "
|
|
177
|
+
"%(message)s"
|
|
178
|
+
),
|
|
179
|
+
) -> Logger:
|
|
180
|
+
_path = os.path.abspath(sys.argv[0])
|
|
181
|
+
_name = os.path.basename(_path)
|
|
182
|
+
_prefix = os.path.splitext(_name)[0]
|
|
183
|
+
_file_name = _prefix + ".log"
|
|
184
|
+
_file_dir = os.path.dirname(_path)
|
|
185
|
+
_file_path = os.path.join(_file_dir, _file_name)
|
|
186
|
+
|
|
187
|
+
if name is None:
|
|
188
|
+
name = _prefix
|
|
189
|
+
_logger = cast(Logger, logging.getLogger(name))
|
|
190
|
+
_logger.setLevel(level)
|
|
191
|
+
|
|
192
|
+
if to_console:
|
|
193
|
+
console_handler_exists = any(isinstance(handler, logging.StreamHandler) for handler in _logger.handlers)
|
|
194
|
+
if not console_handler_exists:
|
|
195
|
+
_set_console_handler(
|
|
196
|
+
_logger,
|
|
197
|
+
console_level=console_level,
|
|
198
|
+
console_fmt=console_fmt
|
|
199
|
+
)
|
|
200
|
+
else:
|
|
201
|
+
for handler in _logger.handlers:
|
|
202
|
+
if isinstance(handler, logging.StreamHandler):
|
|
203
|
+
_logger.removeHandler(handler)
|
|
204
|
+
|
|
205
|
+
if to_file:
|
|
206
|
+
file_handler_exists = any(isinstance(handler, logging.FileHandler) for handler in _logger.handlers)
|
|
207
|
+
if not file_handler_exists:
|
|
208
|
+
if file_path is None:
|
|
209
|
+
file_path = _file_path
|
|
210
|
+
_set_file_handler(
|
|
211
|
+
_logger,
|
|
212
|
+
file_level=file_level,
|
|
213
|
+
file_path=file_path, file_mode=file_mode, file_max_bytes=file_max_bytes,
|
|
214
|
+
file_backup_count=file_backup_count, file_encoding=file_encoding,
|
|
215
|
+
file_fmt=file_fmt
|
|
216
|
+
)
|
|
217
|
+
else:
|
|
218
|
+
for handler in _logger.handlers:
|
|
219
|
+
if isinstance(handler, logging.FileHandler):
|
|
220
|
+
_logger.removeHandler(handler)
|
|
221
|
+
|
|
222
|
+
return _logger
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
logger: Logger = get_logger()
|
|
226
|
+
|
|
227
|
+
__all__ = [
|
|
228
|
+
"LoggerLevel", "Logger", "RotatingFileHandler", "get_logger", "logger"
|
|
229
|
+
]
|