Display TTY  1
Customise your terminal's output
Loading...
Searching...
No Matches
log_level_tracker.py
Go to the documentation of this file.
1"""
2# +==== BEGIN display_tty =================+
3# LOGO:
4# ..@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
5# .@...........................#@
6# @############################.@
7# @...........................@.@
8# @..#######################..@.@
9# @.#########################.@.@
10# @.##>_#####################.@.@
11# @.#########################.@.@
12# @.#########################.@.@
13# @.#########################.@.@
14# @.#########################.@.@
15# @..#######################..@.@
16# @...........................@.@
17# @..+----+______________.....@.@
18# @..+....+______________+....@.@
19# @..+----+...................@.@
20# @...........................@.#
21# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@#.
22# /STOP
23# PROJECT: display_tty
24# FILE: log_level_tracker.py
25# CREATION DATE: 06-11-2025
26# LAST Modified: 12:24:3 06-11-2025
27# DESCRIPTION:
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.
32# /STOP
33# COPYRIGHT: (c) Henry Letellier
34# PURPOSE: File in charge of containing the child that class in charge of tracking the available levels.
35# // AR
36# +==== END display_tty =================+
37"""
38
39from typing import Union, Dict
40import logging
41
42
44 """
45 @class LogLevelTracker
46 @brief Class in charge of tracking the logging levels of the logger library.
47 """
48
49 def __init__(self, bypass_check: bool = False):
50 """
51 @brief Constructor for the LogLevelTracker class.
52 @param bypass_check If True, bypasses the injection of the class into the logging library.
53 """
54 self.levels: Dict = {
55 "DEBUG": logging.DEBUG,
56 "INFO": logging.INFO,
57 "WARN": logging.WARN,
58 "ERROR": logging.ERROR,
59 "CRITICAL": logging.CRITICAL,
60 }
61 if not bypass_check:
62 self.inject_class()
63 return
64
65 class Levels:
66 """
67 @class Levels
68 @brief Class used to patch the small error in the logging library regarding the NOTSET level.
69 """
70
71 CRITICAL = logging.CRITICAL
72 CRIT = CRITICAL
73 FATAL = CRITICAL
74 ERROR = logging.ERROR
75 WARNING = logging.WARNING
76 WARN = WARNING
77 INFO = logging.INFO
78 DEBUG = logging.DEBUG
79 NOTSET = 1
80 __all__ = [
81 CRITICAL,
82 CRIT,
83 FATAL,
84 ERROR,
85 WARNING,
86 WARN,
87 INFO,
88 DEBUG,
89 NOTSET,
90 ]
91
92 def add_level(self, level_name: str, level: int) -> bool:
93 """
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.
98 """
99 if level_name in self.levels:
100 return False
101 self.levels[level_name] = level
102 return True
103
104 def get_level(self, level_name: str) -> Union[int, None]:
105 """
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.
109 """
110 return self.levels.get(level_name, None)
111
112 def get_level_name(self, level: int) -> Union[str, None]:
113 """
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.
117 """
118 for key, value in self.levels.items():
119 if value == level:
120 return key
121 return None
122
123 def check_presence(self) -> bool:
124 """
125 @brief Checks if this class is already present in the logging library.
126 @return True if the class is present, False otherwise.
127 """
128 root_logger = logging.getLogger()
129 if not hasattr(root_logger, "log_level_tracker"):
130 return False
131 return True
132
133 def inject_class(self) -> bool:
134 """
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.
137 """
138 if not self.check_presence():
139 root_logger = logging.getLogger()
140 root_logger.log_level_tracker = self
141 logging.log_level_tracker = self
142 return True
143 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)