simpler-logging 1.0.0__py3-none-any.whl
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.
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
from enum import IntEnum
|
|
3
|
+
from colorama import init, Fore, Style
|
|
4
|
+
from datetime import datetime
|
|
5
|
+
from typing import Protocol, Iterable
|
|
6
|
+
|
|
7
|
+
init()
|
|
8
|
+
|
|
9
|
+
class LogLevel(IntEnum):
|
|
10
|
+
"""Log Levels. Includes DEBUG, INFO, WARN, ERROR, and FATAL in that severity order."""
|
|
11
|
+
DEBUG = 10
|
|
12
|
+
INFO = 20
|
|
13
|
+
WARN = 30
|
|
14
|
+
ERROR = 40
|
|
15
|
+
FATAL = 50
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Handler(Protocol):
|
|
19
|
+
"""
|
|
20
|
+
A Protocol definition for log Handlers.
|
|
21
|
+
|
|
22
|
+
Function following it is expected to be able to receive msg (str), logger_name (str), and do_color (bool).
|
|
23
|
+
"""
|
|
24
|
+
def __call__(self, *, level: LogLevel, msg: str, logger_name: str, do_color: bool) -> None:
|
|
25
|
+
...
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def main_handler(level: str, color: int | str, msg: str, logger_name: str):
|
|
29
|
+
print(f"{color}{Style.DIM}[{datetime.now().astimezone().isoformat(timespec='milliseconds')}]{color} {level} {Style.DIM}{logger_name}{color}: {msg}{Style.RESET_ALL}")
|
|
30
|
+
|
|
31
|
+
def default_handler(*, level: LogLevel, msg: str, logger_name: str, do_color: bool):
|
|
32
|
+
if level == LogLevel.DEBUG:
|
|
33
|
+
main_handler("DEBUG", Fore.CYAN if do_color else "", msg, logger_name)
|
|
34
|
+
elif level == LogLevel.INFO:
|
|
35
|
+
main_handler("INFO", Fore.GREEN if do_color else "", msg, logger_name)
|
|
36
|
+
elif level == LogLevel.WARN:
|
|
37
|
+
main_handler("WARN", Fore.YELLOW if do_color else "", msg, logger_name)
|
|
38
|
+
elif level == LogLevel.ERROR:
|
|
39
|
+
main_handler("ERROR", Fore.RED if do_color else "", msg, logger_name)
|
|
40
|
+
elif level == LogLevel.FATAL:
|
|
41
|
+
main_handler("FATAL", Style.BRIGHT + Fore.LIGHTRED_EX if do_color else "", msg, logger_name)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class Logger:
|
|
46
|
+
"""
|
|
47
|
+
The Logger class. It comes preloaded with default Handlers for all Log Levels.
|
|
48
|
+
"""
|
|
49
|
+
__slots__ = ("_name", "_enabled_levels", "_handlers", "_do_color")
|
|
50
|
+
|
|
51
|
+
def __init__(self, name: str):
|
|
52
|
+
"""Create a new Logger instance."""
|
|
53
|
+
self._name = name
|
|
54
|
+
self._enabled_levels = {LogLevel.INFO, LogLevel.WARN, LogLevel.ERROR, LogLevel.FATAL}
|
|
55
|
+
|
|
56
|
+
self._handlers: dict[LogLevel, Handler] = {LogLevel.DEBUG: default_handler, LogLevel.INFO: default_handler,
|
|
57
|
+
LogLevel.WARN: default_handler, LogLevel.ERROR: default_handler,
|
|
58
|
+
LogLevel.FATAL: default_handler}
|
|
59
|
+
self._do_color = True
|
|
60
|
+
|
|
61
|
+
def log(self, level: LogLevel, msg: str, *args, **kwargs) -> None:
|
|
62
|
+
"""Log a message taking into account the active levels. Generally you should use the dedicated per-level log methods instead."""
|
|
63
|
+
if level in self._enabled_levels:
|
|
64
|
+
self._handlers[level](
|
|
65
|
+
level=level,
|
|
66
|
+
msg=" ".join(
|
|
67
|
+
[msg]
|
|
68
|
+
+ [str(x) for x in args]
|
|
69
|
+
+ [f"{k}={v}" for k, v in kwargs.items()]
|
|
70
|
+
),
|
|
71
|
+
logger_name=self._name,
|
|
72
|
+
do_color=self._do_color,
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@property
|
|
77
|
+
def name(self) -> str:
|
|
78
|
+
"""Get the name of the Logger."""
|
|
79
|
+
return self._name
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def debug(self, msg: str, *args, **kwargs) -> None:
|
|
83
|
+
"""Log a debug message."""
|
|
84
|
+
self.log(LogLevel.DEBUG, msg, *args, **kwargs)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def info(self, msg: str, *args, **kwargs) -> None:
|
|
88
|
+
"""Log an info message."""
|
|
89
|
+
self.log(LogLevel.INFO, msg, *args, **kwargs)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def warn(self, msg: str, *args, **kwargs) -> None:
|
|
93
|
+
"""Log a warning message."""
|
|
94
|
+
self.log(LogLevel.WARN, msg, *args, **kwargs)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def error(self, msg: str, *args, **kwargs) -> None:
|
|
98
|
+
"""Log an error message."""
|
|
99
|
+
self.log(LogLevel.ERROR, msg, *args, **kwargs)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def fatal(self, msg: str, *args, **kwargs) -> None:
|
|
103
|
+
"""Log a fatal message."""
|
|
104
|
+
self.log(LogLevel.FATAL, msg, *args, **kwargs)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def set_color(self, state: bool):
|
|
108
|
+
"""Change whether Handlers should use colors."""
|
|
109
|
+
self._do_color = state
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def debug_handler(self, handler: Handler):
|
|
113
|
+
"""Set the debug Handler"""
|
|
114
|
+
self._handlers[LogLevel.DEBUG] = handler
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def info_handler(self, handler: Handler):
|
|
118
|
+
"""Set the info Handler"""
|
|
119
|
+
self._handlers[LogLevel.INFO] = handler
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def warn_handler(self, handler: Handler):
|
|
123
|
+
"""Set the warn Handler"""
|
|
124
|
+
self._handlers[LogLevel.WARN] = handler
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def error_handler(self, handler: Handler):
|
|
128
|
+
"""Set the error Handler"""
|
|
129
|
+
self._handlers[LogLevel.ERROR] = handler
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def fatal_handler(self, handler: Handler):
|
|
133
|
+
"""Set the fatal Handler"""
|
|
134
|
+
self._handlers[LogLevel.FATAL] = handler
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def enable_level(self, level: LogLevel|Iterable[LogLevel]):
|
|
138
|
+
"""Enable the given Log Level or Set of Log Levels. Does nothing if the Level is already enabled."""
|
|
139
|
+
if isinstance(level, LogLevel):
|
|
140
|
+
self._enabled_levels.add(level)
|
|
141
|
+
else:
|
|
142
|
+
self._enabled_levels.update(level)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def disable_level(self, level: LogLevel|Iterable[LogLevel]):
|
|
146
|
+
"""Disable the given Log Level or Set of Log Levels. Does nothing if the Level is already disabled."""
|
|
147
|
+
if isinstance(level, LogLevel):
|
|
148
|
+
self._enabled_levels.discard(level)
|
|
149
|
+
else:
|
|
150
|
+
self._enabled_levels.difference_update(level)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def toggle_level(self, level: LogLevel|Iterable[LogLevel]):
|
|
154
|
+
"""Toggles the given Log Level or Set of Log Levels."""
|
|
155
|
+
lvl = {level} if isinstance(level, LogLevel) else level
|
|
156
|
+
self._enabled_levels.symmetric_difference_update(lvl)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def min_level(self, level: LogLevel):
|
|
160
|
+
"""Set the minimum Log Level to use. Overwrites previous Log Level Configuration."""
|
|
161
|
+
self._enabled_levels = {x for x in LogLevel if x >= level}
|
simpler_logging/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: simpler-logging
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Simple zero-setup logging.
|
|
5
|
+
Keywords: logging,logger,simple,zero-setup
|
|
6
|
+
Author: minoupower554
|
|
7
|
+
Author-email: minoupower554 <minoupower554@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Topic :: System :: Logging
|
|
13
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Requires-Dist: colorama>=0.4.6
|
|
19
|
+
Requires-Python: >=3.8
|
|
20
|
+
Project-URL: Homepage, https://github.com/minoupower554/simpler-logging
|
|
21
|
+
Project-URL: Issues, https://github.com/minoupower554/simpler-logging/issues
|
|
22
|
+
Project-URL: Repository, https://github.com/minoupower554/simpler-logging
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
This is just a simple zero-setup logging framework.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
simpler_logging/__init__.py,sha256=7ZkQgjgYiUVzuwQfhzG6l94PaiuUQ2Ri1DY15Ck2-Lg,152
|
|
2
|
+
simpler_logging/logger.py,sha256=B_Gt95tchpV2vjqwSRdK6pmRsdb4DVfcwSNiknmpNZ8,5624
|
|
3
|
+
simpler_logging/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
simpler_logging-1.0.0.dist-info/licenses/LICENSE,sha256=3GAyzUZsVt5GiefMrBTF6EM14mRer2plgOHeAt6kqys,1068
|
|
5
|
+
simpler_logging-1.0.0.dist-info/WHEEL,sha256=eycQt0QpYmJMLKpE3X9iDk8R04v2ZF0x82ogq-zP6bQ,79
|
|
6
|
+
simpler_logging-1.0.0.dist-info/METADATA,sha256=NSybLkwn3r_3_grNAEilcH0heQ0jol-drFfqHyxUeag,1025
|
|
7
|
+
simpler_logging-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2026 Minoupower554
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|