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.
15from typing
import Union, Dict
21 @class LogLevelTracker
22 @brief Class in charge of tracking the logging levels of the logger library.
25 def __init__(self, bypass_check: bool =
False):
27 @brief Constructor for the LogLevelTracker class.
28 @param bypass_check If True, bypasses the injection of the class into the logging library.
31 "DEBUG": logging.DEBUG,
34 "ERROR": logging.ERROR,
35 "CRITICAL": logging.CRITICAL,
44 @brief Class used to patch the small error in the logging library regarding the NOTSET level.
47 CRITICAL = logging.CRITICAL
51 WARNING = logging.WARNING
68 def add_level(self, level_name: str, level: int) -> bool:
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.
75 if level_name
in self.
levels:
77 self.
levels[level_name] = level
80 def get_level(self, level_name: str) -> Union[int,
None]:
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.
86 return self.
levels.get(level_name,
None)
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.
94 for key, value
in self.
levels.items():
101 @brief Checks if this class is already present in the logging library.
102 @return True if the class is present, False otherwise.
104 root_logger = logging.getLogger()
105 if not hasattr(root_logger,
"log_level_tracker"):
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.
115 root_logger = logging.getLogger()
116 root_logger.log_level_tracker = self
117 logging.log_level_tracker = self
__init__(self, bool bypass_check=False)
bool check_presence(self)
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)