forked from nyaoouo/FFxivPythonTrigger2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.py
238 lines (179 loc) · 7.31 KB
/
Logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
from inspect import getmodule, stack
from typing import Dict, Set, Callable, Tuple, Annotated, List
from datetime import datetime
from threading import Lock
class ModuleTypeException(Exception):
def __init__(self, module: any):
super(ModuleTypeException, self).__init__("module type [%s] is an invalid logger module type" % module)
class Logger(object):
_module: str
def __init__(self, module_name: str = None):
"""
create a logger to log with a given module name (or auto generated name)(each module name should be unique)
:param module_name: (optional) used module name
"""
if module_name is None:
module_name = getmodule(stack()[1][0]).__name__
if type(module_name) != str:
raise ModuleTypeException(module_name)
self._module = module_name
self.default_level = INFO
def __call__(self, *messages: any) -> None:
"""
log a message in default level~
:param messages: the message elements to be logged
"""
self.log(self.default_level, *messages)
def log(self, level: int, *messages: any) -> None:
"""
log a message~
:param level: level of the log
:param messages: the message elements to be logged
"""
log(level, self._module, *messages)
def debug(self, *messages: any) -> None:
"""
used for debug only, should not used by user
:param messages: the message elements to be logged
"""
self.log(DEBUG, *messages)
def info(self, *messages: any) -> None:
"""
let the user know what happens
:param messages: the message elements to be logged
"""
self.log(INFO, *messages)
def warning(self, *messages: any) -> None:
"""
some thing has gone wrong, but the program is still running
:param messages: the message elements to be logged
"""
self.log(WARNING, *messages)
def error(self, *messages: any) -> None:
"""
some error happen, some function will be affected
:param messages: the message elements to be logged
"""
self.log(ERROR, *messages)
def critical(self, *messages: any) -> None:
"""
a fatal error happen, may cause the program to terminate
:param messages: the message elements to be logged
"""
self.log(CRITICAL, *messages)
class Log(object):
level: int
module: str
message: str
datetime: datetime
def __init__(self, level: int, module: str, message: str):
self.level = level
self.module = module
self.message = message
self.datetime = datetime.now()
def header(self):
return LOG_HEADER_FORMAT.format(
time=self.datetime.strftime(TIME_FORMAT)[:-TIME_CUTOFF],
module=self.module,
level=_get_level_name(self.level),
)
def __str__(self):
return LOG_FORMAT.format(
header=self.header(),
message=self.message,
)
def _default_log_error_handler(e: Exception) -> None:
pass
def _get_level_name(level: any) -> str:
result = _level_name.get(level)
if result is not None:
return result
return "Level %s" % level
def log(level: int, module: str, *messages: any) -> None:
"""
log a message
*suggested to use the Logger class instead of this
:param level: level of the log
:param module: an identifier of where the log from
:param messages: the message elements to be logged
"""
msg_log = Log(level, module, MESSAGES_SEPARATOR.join(map(str, messages)))
if level >= print_log_level >= 0:
with _print_lock:
if len(print_history) > STORE_MAX:
temp = print_history[len(print_history) // 10:]
print_history.clear()
print_history.extend(temp)
print_history.append(msg_log)
for line in msg_log.message.split('\n'):
PRINT(LOG_FORMAT.format(header=msg_log.header(), message=line))
for h_lv, handler in log_handler:
if level >= h_lv:
try:
handler(msg_log)
except Exception as e:
exception_handler(e)
def debug(module: str, *messages: any) -> None:
"""
used for debug only, should not used by user
*suggested to use the Logger class instead of this
:param module: an identifier of where the log from
:param messages: the message elements to be logged
"""
log(DEBUG, module, *messages)
def info(module: str, *messages: any) -> None:
"""
let the user know what happens
*suggested to use the Logger class instead of this
:param module: an identifier of where the log from
:param messages: the message elements to be logged
"""
log(INFO, module, *messages)
def warning(module: str, *messages: any) -> None:
"""
some thing has gone wrong, but the program is still running
*suggested to use the Logger class instead of this
:param module: an identifier of where the log from
:param messages: the message elements to be logged
"""
log(WARNING, module, *messages)
def error(module: str, *messages: any) -> None:
"""
some error happen, some function will be affected
*suggested to use the Logger class instead of this
:param module: an identifier of where the log from
:param messages: the message elements to be logged
"""
log(ERROR, module, *messages)
def critical(module: str, *messages: any) -> None:
"""
a fatal error happen, may cause the program to terminate
*suggested to use the Logger class instead of this
:param module: an identifier of where the log from
:param messages: the message elements to be logged
"""
log(CRITICAL, module, *messages)
DEBUG: Annotated[int, "used for debug only, should not used by user"] = 10
INFO: Annotated[int, "let the user know what happens"] = 20
WARNING: Annotated[int, "some thing has gone wrong, but the program is still running"] = 30
ERROR: Annotated[int, "some error happen, some function will be affected"] = 40
CRITICAL: Annotated[int, "a fatal error happen, may cause the program to terminate"] = 50
_level_name: Dict[int, str] = {
CRITICAL: 'CRITICAL',
ERROR: 'ERROR',
WARNING: 'WARNING',
INFO: 'INFO',
DEBUG: 'DEBUG',
}
MESSAGES_SEPARATOR: Annotated[str, "the separator for logger used to connect messages"] = "\t"
LOG_HEADER_FORMAT: Annotated[str, "the format of log header"] = "[{level}]\t[{time}|{module}]\t"
LOG_FORMAT: Annotated[str, "the format of log"] = "{header}\t{message}"
TIME_FORMAT: Annotated[str, "the format of time"] = "%Y-%m-%d %H:%M:%S.%f"
TIME_CUTOFF: Annotated[int, "cutoff the time string"] = 3
STORE_MAX: Annotated[int, "max volume of the history storage"] = 2000
PRINT: Annotated[Callable[[any], None], "the method called for print user seen log"] = print
print_log_level: Annotated[int, "if the log level is lower then this, it wont be printed"] = INFO
log_handler: Annotated[Set[Tuple[int, Callable[[Log], None]]], "a set of handler in (handle level, handle method)"] = set()
exception_handler: Annotated[Callable[[Exception], None], "the handler of exceptions happen in log handlers"] = _default_log_error_handler
print_history: Annotated[List[Log], "store the log have printed"] = list()
_print_lock = Lock()