2# +==== BEGIN display_tty =================+
4# ..@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
5# .@...........................#@
6# @############################.@
7# @...........................@.@
8# @..#######################..@.@
9# @.#########################.@.@
10# @.##>_#####################.@.@
11# @.#########################.@.@
12# @.#########################.@.@
13# @.#########################.@.@
14# @.#########################.@.@
15# @..#######################..@.@
16# @...........................@.@
17# @..+----+______________.....@.@
18# @..+....+______________+....@.@
19# @..+----+...................@.@
20# @...........................@.#
21# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@#.
24# FILE: log_level_tracker.py
25# CREATION DATE: 06-11-2025
26# LAST Modified: 12:24:3 06-11-2025
28# A module that allows you to display text with a few boilers (i.e. put your text in a square for titles). It also allows to log to the terminal by wrapping around the logging library.
29# @file log_level_tracker.py
30# @brief Provides the LogLevelTracker class to manage and track logging levels in the logging library.
31# @details This module allows adding custom logging levels, retrieving levels by name or value, and injecting the LogLevelTracker class into the logging library for extended functionality.
33# COPYRIGHT: (c) Henry Letellier
34# PURPOSE: File in charge of containing the child that class in charge of tracking the available levels.
36# +==== END display_tty =================+
39from typing
import Union, Dict
45 @class LogLevelTracker
46 @brief Class in charge of tracking the logging levels of the logger library.
49 def __init__(self, bypass_check: bool =
False):
51 @brief Constructor for the LogLevelTracker class.
52 @param bypass_check If True, bypasses the injection of the class into the logging library.
55 "DEBUG": logging.DEBUG,
58 "ERROR": logging.ERROR,
59 "CRITICAL": logging.CRITICAL,
68 @brief Class used to patch the small error in the logging library regarding the NOTSET level.
71 CRITICAL = logging.CRITICAL
75 WARNING = logging.WARNING
92 def add_level(self, level_name: str, level: int) -> bool:
94 @brief Adds a new logging level.
95 @param level_name The name of the new logging level.
96 @param level The integer value of the new logging level.
97 @return True if the level was added successfully, False if the level already exists.
99 if level_name
in self.
levels:
101 self.
levels[level_name] = level
104 def get_level(self, level_name: str) -> Union[int,
None]:
106 @brief Retrieves the logging level for a given level name.
107 @param level_name The name of the logging level.
108 @return The integer value of the logging level, or None if the level name does not exist.
110 return self.
levels.get(level_name,
None)
114 @brief Retrieves the logging level name for a given level value.
115 @param level The integer value of the logging level.
116 @return The name of the logging level, or None if the level value does not exist.
118 for key, value
in self.
levels.items():
125 @brief Checks if this class is already present in the logging library.
126 @return True if the class is present, False otherwise.
128 root_logger = logging.getLogger()
129 if not hasattr(root_logger,
"log_level_tracker"):
135 @brief Injects this class into the logging library if it is not already present.
136 @return True if the class was successfully injected, False otherwise.
139 root_logger = logging.getLogger()
140 root_logger.log_level_tracker = self
141 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)