Display TTY  1
Customise your terminal's output
Loading...
Searching...
No Matches
log_level_tracker.py
Go to the documentation of this file.
7
8"""
9@file log_level_tracker.py
10@brief Provides the LogLevelTracker class to manage and track logging levels in the logging library.
11@details This module allows adding custom logging levels, retrieving levels by name or value,
12and injecting the LogLevelTracker class into the logging library for extended functionality.
13"""
14
15from typing import Union, Dict
16import logging
17
18
20 """
21 @class LogLevelTracker
22 @brief Class in charge of tracking the logging levels of the logger library.
23 """
24
25 def __init__(self, bypass_check: bool = False):
26 """
27 @brief Constructor for the LogLevelTracker class.
28 @param bypass_check If True, bypasses the injection of the class into the logging library.
29 """
30 self.levels: Dict = {
31 "DEBUG": logging.DEBUG,
32 "INFO": logging.INFO,
33 "WARN": logging.WARN,
34 "ERROR": logging.ERROR,
35 "CRITICAL": logging.CRITICAL,
36 }
37 if not bypass_check:
38 self.inject_class()
39 return
40
41 class Levels:
42 """
43 @class Levels
44 @brief Class used to patch the small error in the logging library regarding the NOTSET level.
45 """
46
47 CRITICAL = logging.CRITICAL
48 CRIT = CRITICAL
49 FATAL = CRITICAL
50 ERROR = logging.ERROR
51 WARNING = logging.WARNING
52 WARN = WARNING
53 INFO = logging.INFO
54 DEBUG = logging.DEBUG
55 NOTSET = 1
56 __all__ = [
57 CRITICAL,
58 CRIT,
59 FATAL,
60 ERROR,
61 WARNING,
62 WARN,
63 INFO,
64 DEBUG,
65 NOTSET,
66 ]
67
68 def add_level(self, level_name: str, level: int) -> bool:
69 """
70 @brief Adds a new logging level.
71 @param level_name The name of the new logging level.
72 @param level The integer value of the new logging level.
73 @return True if the level was added successfully, False if the level already exists.
74 """
75 if level_name in self.levels:
76 return False
77 self.levels[level_name] = level
78 return True
79
80 def get_level(self, level_name: str) -> Union[int, None]:
81 """
82 @brief Retrieves the logging level for a given level name.
83 @param level_name The name of the logging level.
84 @return The integer value of the logging level, or None if the level name does not exist.
85 """
86 return self.levels.get(level_name, None)
87
88 def get_level_name(self, level: int) -> Union[str, None]:
89 """
90 @brief Retrieves the logging level name for a given level value.
91 @param level The integer value of the logging level.
92 @return The name of the logging level, or None if the level value does not exist.
93 """
94 for key, value in self.levels.items():
95 if value == level:
96 return key
97 return None
98
99 def check_presence(self) -> bool:
100 """
101 @brief Checks if this class is already present in the logging library.
102 @return True if the class is present, False otherwise.
103 """
104 root_logger = logging.getLogger()
105 if not hasattr(root_logger, "log_level_tracker"):
106 return False
107 return True
108
109 def inject_class(self) -> bool:
110 """
111 @brief Injects this class into the logging library if it is not already present.
112 @return True if the class was successfully injected, False otherwise.
113 """
114 if not self.check_presence():
115 root_logger = logging.getLogger()
116 root_logger.log_level_tracker = self
117 logging.log_level_tracker = self
118 return True
119 return False
bool add_level(self, str level_name, int level)
Union[int, None] get_level(self, str level_name)
Union[str, None] get_level_name(self, int level)