|
1
|
|
|
""" |
|
2
|
|
|
A simple consoled based logging utility. |
|
3
|
|
|
|
|
4
|
|
|
Example usage: |
|
5
|
|
|
>>> from logging import log |
|
6
|
|
|
|
|
7
|
|
|
>>> log.success("Connected to the cookie generator.") |
|
8
|
|
|
>>> log.inform("Sigmanificient took generated a cookie.") |
|
9
|
|
|
>>> log.warn("No more cookies can be generated due to empty Stack.") |
|
10
|
|
|
>>> log.error("Missing cookie generator key.") |
|
11
|
|
|
>>> log.db( |
|
12
|
|
|
>>> ... "Attempting `select cookie from user where user_name = ?" |
|
13
|
|
|
>>> ... "with args ?= `Sigmanificient`" |
|
14
|
|
|
>>> ... ) |
|
15
|
|
|
|
|
16
|
|
|
""" |
|
17
|
|
|
|
|
18
|
|
|
from datetime import datetime |
|
19
|
|
|
|
|
20
|
|
|
import colorama |
|
21
|
|
|
from termcolor import colored |
|
22
|
|
|
|
|
23
|
|
|
COMMON_LOG_FORMAT: str = "%d/%b/%Y-%Hh:%Mm:%Ss" |
|
24
|
|
|
|
|
25
|
|
|
colorama.init() |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
class Logger: |
|
29
|
|
|
"""The utility class for easy and beautiful logging. |
|
30
|
|
|
|
|
31
|
|
|
This class should not be instantiated. |
|
32
|
|
|
Instead use the log instance given in the module. |
|
33
|
|
|
""" |
|
34
|
|
|
|
|
35
|
|
|
@staticmethod |
|
36
|
|
|
def __log(color, log_type, message) -> None: |
|
37
|
|
|
date: str = datetime.now().strftime(COMMON_LOG_FORMAT) |
|
38
|
|
|
|
|
39
|
|
|
print( |
|
40
|
|
|
f"[{colored(date, 'magenta')}] [{colored(log_type, color=color)}]", |
|
41
|
|
|
message, flush=True |
|
42
|
|
|
) |
|
43
|
|
|
|
|
44
|
|
|
def success(self, message: str) -> None: |
|
45
|
|
|
"""Log a successful operation. |
|
46
|
|
|
|
|
47
|
|
|
:param message: str |
|
48
|
|
|
details of the operation. |
|
49
|
|
|
""" |
|
50
|
|
|
self.__log('green', 'Success', message) |
|
51
|
|
|
|
|
52
|
|
|
def inform(self, message: str) -> None: |
|
53
|
|
|
"""Log a informative or debug message. |
|
54
|
|
|
|
|
55
|
|
|
:param message: str |
|
56
|
|
|
details of the message. |
|
57
|
|
|
""" |
|
58
|
|
|
self.__log('blue', 'Info', message) |
|
59
|
|
|
|
|
60
|
|
|
def warn(self, message: str) -> None: |
|
61
|
|
|
"""Log a unexpected behavior that isn't fatal. |
|
62
|
|
|
|
|
63
|
|
|
:param message: |
|
64
|
|
|
Failure details or error __cause__. |
|
65
|
|
|
""" |
|
66
|
|
|
self.__log('yellow', 'Warning', message) |
|
67
|
|
|
|
|
68
|
|
|
def error(self, message: str) -> None: |
|
69
|
|
|
"""Log a fatal operation or missing core element |
|
70
|
|
|
cause impossibility to run or continue. |
|
71
|
|
|
|
|
72
|
|
|
:param message: |
|
73
|
|
|
Failure details, error __cause__ and/or solve hint. |
|
74
|
|
|
""" |
|
75
|
|
|
self.__log('red', 'Error', message) |
|
76
|
|
|
quit() |
|
77
|
|
|
|
|
78
|
|
|
def db(self, message: str) -> None: |
|
79
|
|
|
"""Log a db call. |
|
80
|
|
|
|
|
81
|
|
|
:param message: |
|
82
|
|
|
Query string or result. |
|
83
|
|
|
""" |
|
84
|
|
|
self.__log('white', 'DB', message) |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
log = Logger() |
|
88
|
|
|
|