Terarea  2
The automation project
Loading...
Searching...
No Matches
logger.py
Go to the documentation of this file.
1"""_summary_
2 File in charge of logging events into the logging database
3"""
4from typing import List, Union, Dict, Any
5
6from display_tty import Disp, TOML_CONF, FILE_DESCRIPTOR, SAVE_TO_FILE, FILE_NAME
7
8from ..components.runtime_data import RuntimeData
9from ..components import constants as CONST
10
11from . import constants as ACONST
12
13
15 """_summary_
16 Class in charge of logging events into the logging database
17 """
18
19 def __init__(self, runtime_data: RuntimeData, success: int = 0, error: int = 84, debug: bool = False) -> None:
20 """_summary_
21 Class in charge of logging events into the logging database
22
23 Args:
24 runtime_data (RuntimeData): _description_
25 success (int, optional): _description_. Defaults to 0.
26 error (int, optional): _description_. Defaults to 84.
27 debug (bool, optional): _description_. Defaults to False.
28 """
29 # -------------------------- Inherited values --------------------------
30 self.error = error
31 self.debug = debug
32 self.success = success
33 self.runtime_data = runtime_data
34 # ---------------------- The visual logger class ----------------------
35 self.disp: Disp = Disp(
36 TOML_CONF,
37 SAVE_TO_FILE,
38 FILE_NAME,
39 FILE_DESCRIPTOR,
40 debug=self.debug,
41 logger=self.__class__.__name__
42 )
43
44 def log_event(self, log_type: str, action_id: int = 0, code: int = ACONST.CODE_ERROR, message: Union[str, None] = None, resolved: bool = False) -> int:
45 """_summary_
46 Log an event into the logging database
47
48 Args:
49 log_type (str): _description_: The type of the event
50 action_id (int, optional): _description_: The id of the action that triggered the event, defaults to 0
51 code (int, optional): _description_: The code of the event, defaults to ACONST.CODE_ERROR
52 message (Union[str, None], optional): _description_: The message of the event, defaults to None
53 resolved (bool, optional): _description_: The status of the event, defaults to False
54
55 Returns:
56 int: _description_: Returns 0 if it succeeds, 84 otherwise
57 """
58 title = "log_event"
59
60 columns: List[str] = self.runtime_data.database_link.get_table_column_names(
61 CONST.TAB_ACTION_LOGGING
62 )
63 if isinstance(columns, int) is True and columns != self.success:
64 self.disp.log_error("Failed to get the table columns.", title)
65 return self.error
66 columns.pop(0)
67
68 if log_type not in ACONST.LIST_TYPE:
69 msg = f"Type: {type} is not in the list of types."
70 msg += f" Setting type to {ACONST.TYPE_UNKNOWN}."
71 self.disp.log_warning(msg, title)
72 log_type = ACONST.TYPE_UNKNOWN
73
74 if self._check_if_action_id_in_table(action_id) is False:
75 msg = f"The action_id, {action_id},"
76 msg += " is not in the Actions table."
77 self.disp.log_error(msg, title)
78 return self.error
79
80 self.disp.log_debug(f"code = {code}", title)
81
82 if code in ACONST.LOG_EQUIVALENCE:
83 code_level = ACONST.LOG_EQUIVALENCE[code]
84 else:
85 code_level = ACONST.LOG_EQUIVALENCE[ACONST.CODE_UNKNOWN]
86
87 self.disp.log_debug(f"code_level = {code_level}", title)
88
89 if message is None:
90 self.disp.log_warning(
91 "The message is None, defaulting to the code message equivalence.", title
92 )
93 message = ACONST.LOG_MESSAGE_EQUIVALENCE[code_level]
94
95 self.disp.log_debug(f"message = {message}", title)
96
97 if isinstance(resolved, bool) is False:
98 self.disp.log_warning(
99 "The resolved status is not a boolean, defaulting to False.", title
100 )
101 resolved = False
102
103 self.disp.log_debug(f"resolved = {resolved}", title)
104
105 data = [
106 "now", # time
107 f"{log_type}", # type
108 f"{action_id}", # action_id
109 f"{message}", # message
110 f"{code}", # error_code
111 f"{code_level}", # error_level
112 f"{int(resolved)}" # resolved
113 ]
114 self.disp.log_debug(f"data = {data}", title)
115 status = self.runtime_data.database_link.insert_data_into_table(
116 table=CONST.TAB_ACTION_LOGGING,
117 data=data,
118 column=columns
119 )
120 self.disp.log_info(f"status = {status}", title)
121 if status != self.success:
122 self.disp.log_error("Failed to log the event.", title)
123 return self.error
124 return self.success
125
126 def _check_if_action_id_in_table(self, action_id: int = 0) -> bool:
127 """_summary_
128 Check if the action_id is in the Actions table
129
130 Args:
131 action_id (int, optional): _description_. Defaults to 0.: The id of the concerned action.
132
133 Returns:
134 bool: _description_: Returns True if it is present, False otherwise.
135 """
136 title = "_check_if_action_id_in_table"
137 self.disp.log_debug(f"action_id = {action_id}", title)
138 data = self.runtime_data.database_link.get_data_from_table(
139 table=CONST.TAB_ACTIONS,
140 column="id",
141 where=f"id='{action_id}'",
142 beautify=False
143 )
144 self.disp.log_debug(f"data = {data}", title)
145 if isinstance(data, int) is True and data != self.success:
146 return False
147 self.disp.log_debug(f"len(data) = {len(data)}", title)
148 if len(data) == 1:
149 return True
150 return False
151
152 def log_info(self, log_type: str, action_id: int = 0, message: Union[str, None] = None, resolved: bool = False) -> int:
153 """_summary_
154 Log an info event into the logging database
155
156 Args:
157 log_type (str): _description_: The type of the event
158 action_id (int, optional): _description_: The id of the action that triggered the event, defaults to 0
159 message (Union[str, None], optional): _description_: The message of the event, defaults to None
160 resolved (bool, optional): _description_: The status of the event, defaults to False
161
162 Returns:
163 int: _description_: Returns self.success if it succeeds, self.error otherwise
164 """
165 return self.log_event(
166 log_type=log_type,
167 action_id=action_id,
168 code=ACONST.CODE_INFO,
169 message=message,
170 resolved=resolved
171 )
172
173 def log_success(self, log_type: str, action_id: int = 0, message: Union[str, None] = None, resolved: bool = False) -> int:
174 """_summary_
175 Log an success event into the logging database
176
177 Args:
178 log_type (str): _description_: The type of the event
179 action_id (int, optional): _description_: The id of the action that triggered the event, defaults to 0
180 message (Union[str, None], optional): _description_: The message of the event, defaults to None
181 resolved (bool, optional): _description_: The status of the event, defaults to False
182
183 Returns:
184 int: _description_: Returns self.success if it succeeds, self.error otherwise
185 """
186 return self.log_event(
187 log_type=log_type,
188 action_id=action_id,
189 code=ACONST.CODE_SUCCESS,
190 message=message,
191 resolved=resolved
192 )
193
194 def log_debug(self, log_type: str, action_id: int = 0, message: Union[str, None] = None, resolved: bool = False) -> int:
195 """_summary_
196 Log an debug event into the logging database
197
198 Args:
199 log_type (str): _description_: The type of the event
200 action_id (int, optional): _description_: The id of the action that triggered the event, defaults to 0
201 message (Union[str, None], optional): _description_: The message of the event, defaults to None
202 resolved (bool, optional): _description_: The status of the event, defaults to False
203
204 Returns:
205 int: _description_: Returns self.success if it succeeds, self.error otherwise
206 """
207 return self.log_event(
208 log_type=log_type,
209 action_id=action_id,
210 code=ACONST.CODE_DEBUG,
211 message=message,
212 resolved=resolved
213 )
214
215 def log_warning(self, log_type: str, action_id: int = 0, message: Union[str, None] = None, resolved: bool = False) -> int:
216 """_summary_
217 Log an warning event into the logging database
218
219 Args:
220 log_type (str): _description_: The type of the event
221 action_id (int, optional): _description_: The id of the action that triggered the event, defaults to 0
222 message (Union[str, None], optional): _description_: The message of the event, defaults to None
223 resolved (bool, optional): _description_: The status of the event, defaults to False
224
225 Returns:
226 int: _description_: Returns self.success if it succeeds, self.error otherwise
227 """
228 return self.log_event(
229 log_type=log_type,
230 action_id=action_id,
231 code=ACONST.CODE_WARNING,
232 message=message,
233 resolved=resolved
234 )
235
236 def log_error(self, log_type: str, action_id: int = 0, message: Union[str, None] = None, resolved: bool = False) -> int:
237 """_summary_
238 Log an error event into the logging database
239
240 Args:
241 log_type (str): _description_: The type of the event
242 action_id (int, optional): _description_: The id of the action that triggered the event, defaults to 0
243 message (Union[str, None], optional): _description_: The message of the event, defaults to None
244 resolved (bool, optional): _description_: The status of the event, defaults to False
245
246 Returns:
247 int: _description_: Returns self.success if it succeeds, self.error otherwise
248 """
249 return self.log_event(
250 log_type=log_type,
251 action_id=action_id,
252 code=ACONST.CODE_ERROR,
253 message=message,
254 resolved=resolved
255 )
256
257 def log_critical(self, log_type: str, action_id: int = 0, message: Union[str, None] = None, resolved: bool = False) -> int:
258 """_summary_
259 Log an critical event into the logging database
260
261 Args:
262 log_type (str): _description_: The type of the event
263 action_id (int, optional): _description_: The id of the action that triggered the event, defaults to 0
264 message (Union[str, None], optional): _description_: The message of the event, defaults to None
265 resolved (bool, optional): _description_: The status of the event, defaults to False
266
267 Returns:
268 int: _description_: Returns self.success if it succeeds, self.error otherwise
269 """
270 return self.log_event(
271 log_type=log_type,
272 action_id=action_id,
273 code=ACONST.CODE_CRITICAL,
274 message=message,
275 resolved=resolved
276 )
277
278 def log_fatal(self, log_type: str, action_id: int = 0, message: Union[str, None] = None, resolved: bool = False) -> int:
279 """_summary_
280 Log an fatal event into the logging database
281
282 Args:
283 log_type (str): _description_: The type of the event
284 action_id (int, optional): _description_: The id of the action that triggered the event, defaults to 0
285 message (Union[str, None], optional): _description_: The message of the event, defaults to None
286 resolved (bool, optional): _description_: The status of the event, defaults to False
287
288 Returns:
289 int: _description_: Returns self.success if it succeeds, self.error otherwise
290 """
291 return self.log_event(
292 log_type=log_type,
293 action_id=action_id,
294 code=ACONST.CODE_FATAL,
295 message=message,
296 resolved=resolved
297 )
298
299 def log_unknown(self, log_type: str, action_id: int = 0, message: Union[str, None] = None, resolved: bool = False) -> int:
300 """_summary_
301 Log an unknown event into the logging database
302
303 Args:
304 log_type (str): _description_: The type of the event
305 action_id (int, optional): _description_: The id of the action that triggered the event, defaults to 0
306 message (Union[str, None], optional): _description_: The message of the event, defaults to None
307 resolved (bool, optional): _description_: The status of the event, defaults to False
308
309 Returns:
310 int: _description_: Returns self.success if it succeeds, self.error otherwise
311 """
312 return self.log_event(
313 log_type=log_type,
314 action_id=action_id,
315 code=ACONST.CODE_UNKNOWN,
316 message=message,
317 resolved=resolved
318 )
319
320 def get_logs(self, action_id: Union[int, None] = None, code: Union[int, None] = None, beautify: bool = True) -> Union[List[Union[Dict[str, Any], List[Any]]], int]:
321 """_summary_
322 Get the logs from the database
323
324 Args:
325 action_id (Union[int, None], optional): _description_: Specify an action id to narrow down the results.
326 code (Union[int, None], optional): _description_: Specify a code to narrow down the results.
327 beautify (bool, optional): _description_: Set to True if you wish to beautify the output
328
329 Returns:
330 Union[List[Dict[str, Any]], int]: _description_: Returns the logs if it succeeds, self.error otherwise
331 """
332 title = "get_logs"
333 where = []
334 if action_id is not None:
335 where.append(f"action_id='{action_id}'")
336 if code is not None:
337 where.append(f"code='{code}'")
338 if len(where) == 0:
339 where = None
340 self.disp.log_debug(f"where = {where}", title)
341 data = self.runtime_data.database_link.get_data_from_table(
342 table=CONST.TAB_ACTION_LOGGING,
343 column="*",
344 where=where,
345 beautify=beautify
346 )
347 self.disp.log_debug(f"data = {data}", title)
348 if isinstance(data, int) is True and data != self.success:
349 self.disp.log_error("Failed to get the logs.", title)
350 return self.error
351 return data
352
353 def get_logs_unknown(self, action_id: Union[int, None] = None, beautify: bool = True) -> Union[List[Union[Dict[str, Any], List[Any]]], int]:
354 """_summary_
355 Get the unknown logs from the database
356
357 Args:
358 action_id (Union[int, None], optional): _description_: Specify an action id to narrow down the results.
359 beautify (bool, optional): _description_: Set to True if you wish to beautify the output
360
361 Returns:
362 Union[List[Dict[str, Any]], int]: _description_: Returns the logs if it succeeds, self.error otherwise
363 """
364 return self.get_logs(
365 action_id=action_id,
366 code=ACONST.CODE_UNKNOWN,
367 beautify=beautify
368 )
369
370 def get_logs_info(self, action_id: Union[int, None] = None, beautify: bool = True) -> Union[List[Union[Dict[str, Any], List[Any]]], int]:
371 """_summary_
372 Get the info logs from the database
373
374 Args:
375 action_id (Union[int, None], optional): _description_: Specify an action id to narrow down the results.
376 beautify (bool, optional): _description_: Set to True if you wish to beautify the output
377
378 Returns:
379 Union[List[Dict[str, Any]], int]: _description_: Returns the logs if it succeeds, self.error otherwise
380 """
381 return self.get_logs(
382 action_id=action_id,
383 code=ACONST.CODE_INFO,
384 beautify=beautify
385 )
386
387 def get_logs_success(self, action_id: Union[int, None] = None, beautify: bool = True) -> Union[List[Union[Dict[str, Any], List[Any]]], int]:
388 """_summary_
389 Get the success logs from the database
390
391 Args:
392 action_id (Union[int, None], optional): _description_: Specify an action id to narrow down the results.
393 beautify (bool, optional): _description_: Set to True if you wish to beautify the output
394
395 Returns:
396 Union[List[Dict[str, Any]], int]: _description_: Returns the logs if it succeeds, self.error otherwise
397 """
398 return self.get_logs(
399 action_id=action_id,
400 code=ACONST.CODE_SUCCESS,
401 beautify=beautify
402 )
403
404 def get_logs_debug(self, action_id: Union[int, None] = None, beautify: bool = True) -> Union[List[Union[Dict[str, Any], List[Any]]], int]:
405 """_summary_
406 Get the debug logs from the database
407
408 Args:
409 action_id (Union[int, None], optional): _description_: Specify an action id to narrow down the results.
410 beautify (bool, optional): _description_: Set to True if you wish to beautify the output
411
412 Returns:
413 Union[List[Dict[str, Any]], int]: _description_: Returns the logs if it succeeds, self.error otherwise
414 """
415 return self.get_logs(
416 action_id=action_id,
417 code=ACONST.CODE_DEBUG,
418 beautify=beautify
419 )
420
421 def get_logs_warning(self, action_id: Union[int, None] = None, beautify: bool = True) -> Union[List[Union[Dict[str, Any], List[Any]]], int]:
422 """_summary_
423 Get the warning logs from the database
424
425 Args:
426 action_id (Union[int, None], optional): _description_: Specify an action id to narrow down the results.
427 beautify (bool, optional): _description_: Set to True if you wish to beautify the output
428
429 Returns:
430 Union[List[Dict[str, Any]], int]: _description_: Returns the logs if it succeeds, self.error otherwise
431 """
432 return self.get_logs(
433 action_id=action_id,
434 code=ACONST.CODE_WARNING,
435 beautify=beautify
436 )
437
438 def get_logs_error(self, action_id: Union[int, None] = None, beautify: bool = True) -> Union[List[Union[Dict[str, Any], List[Any]]], int]:
439 """_summary_
440 Get the error logs from the database
441
442 Args:
443 action_id (Union[int, None], optional): _description_: Specify an action id to narrow down the results.
444 beautify (bool, optional): _description_: Set to True if you wish to beautify the output
445
446 Returns:
447 Union[List[Dict[str, Any]], int]: _description_: Returns the logs if it succeeds, self.error otherwise
448 """
449 return self.get_logs(
450 action_id=action_id,
451 code=ACONST.CODE_ERROR,
452 beautify=beautify
453 )
454
455 def get_logs_critical(self, action_id: Union[int, None] = None, beautify: bool = True) -> Union[List[Union[Dict[str, Any], List[Any]]], int]:
456 """_summary_
457 Get the critical logs from the database
458
459 Args:
460 action_id (Union[int, None], optional): _description_: Specify an action id to narrow down the results.
461 beautify (bool, optional): _description_: Set to True if you wish to beautify the output
462
463 Returns:
464 Union[List[Dict[str, Any]], int]: _description_: Returns the logs if it succeeds, self.error otherwise
465 """
466 return self.get_logs(
467 action_id=action_id,
468 code=ACONST.CODE_CRITICAL,
469 beautify=beautify
470 )
471
472 def get_logs_fatal(self, action_id: Union[int, None] = None, beautify: bool = True) -> Union[List[Union[Dict[str, Any], List[Any]]], int]:
473 """_summary_
474 Get the fatal logs from the database
475
476 Args:
477 action_id (Union[int, None], optional): _description_: Specify an action id to narrow down the results.
478 beautify (bool, optional): _description_: Set to True if you wish to beautify the output
479
480 Returns:
481 Union[List[Dict[str, Any]], int]: _description_: Returns the logs if it succeeds, self.error otherwise
482 """
483 return self.get_logs(
484 action_id=action_id,
485 code=ACONST.CODE_FATAL,
486 beautify=beautify
487 )
int log_error(self, str log_type, int action_id=0, Union[str, None] message=None, bool resolved=False)
Definition logger.py:236
int log_critical(self, str log_type, int action_id=0, Union[str, None] message=None, bool resolved=False)
Definition logger.py:257
int log_unknown(self, str log_type, int action_id=0, Union[str, None] message=None, bool resolved=False)
Definition logger.py:299
Union[List[Union[Dict[str, Any], List[Any]]], int] get_logs_warning(self, Union[int, None] action_id=None, bool beautify=True)
Definition logger.py:421
int log_warning(self, str log_type, int action_id=0, Union[str, None] message=None, bool resolved=False)
Definition logger.py:215
Union[List[Union[Dict[str, Any], List[Any]]], int] get_logs_unknown(self, Union[int, None] action_id=None, bool beautify=True)
Definition logger.py:353
int log_fatal(self, str log_type, int action_id=0, Union[str, None] message=None, bool resolved=False)
Definition logger.py:278
Union[List[Union[Dict[str, Any], List[Any]]], int] get_logs_critical(self, Union[int, None] action_id=None, bool beautify=True)
Definition logger.py:455
bool _check_if_action_id_in_table(self, int action_id=0)
Definition logger.py:126
int log_info(self, str log_type, int action_id=0, Union[str, None] message=None, bool resolved=False)
Definition logger.py:152
Union[List[Union[Dict[str, Any], List[Any]]], int] get_logs_error(self, Union[int, None] action_id=None, bool beautify=True)
Definition logger.py:438
int log_success(self, str log_type, int action_id=0, Union[str, None] message=None, bool resolved=False)
Definition logger.py:173
Union[List[Union[Dict[str, Any], List[Any]]], int] get_logs_debug(self, Union[int, None] action_id=None, bool beautify=True)
Definition logger.py:404
None __init__(self, RuntimeData runtime_data, int success=0, int error=84, bool debug=False)
Definition logger.py:19
Union[List[Union[Dict[str, Any], List[Any]]], int] get_logs_info(self, Union[int, None] action_id=None, bool beautify=True)
Definition logger.py:370
int log_debug(self, str log_type, int action_id=0, Union[str, None] message=None, bool resolved=False)
Definition logger.py:194
Union[List[Union[Dict[str, Any], List[Any]]], int] get_logs(self, Union[int, None] action_id=None, Union[int, None] code=None, bool beautify=True)
Definition logger.py:320
Union[List[Union[Dict[str, Any], List[Any]]], int] get_logs_success(self, Union[int, None] action_id=None, bool beautify=True)
Definition logger.py:387
Union[List[Union[Dict[str, Any], List[Any]]], int] get_logs_fatal(self, Union[int, None] action_id=None, bool beautify=True)
Definition logger.py:472
int log_event(self, str log_type, int action_id=0, int code=ACONST.CODE_ERROR, Union[str, None] message=None, bool resolved=False)
Definition logger.py:44