Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 4aeded7

Browse files
Update logging methods to support multiple styles in type hints
1 parent 3430978 commit 4aeded7

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

ExceptionHandler/handler.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import datetime
33
import sys
44
import re
5+
from typing import Callable
6+
57
from ExceptionHandler.colors import Colors, Format
68
from ExceptionHandler.messages import GENERIC_MESSAGES
79
from TerminalPrint import TPrint
@@ -13,40 +15,40 @@ def __init__(self,
1315
trace: bool = False,
1416
use_timestamp: bool = False,
1517
exit_script: bool = False,
16-
print_function: callable = TPrint().critical,
18+
print_function: Callable = TPrint().critical,
1719
return_string_rather_than_print: bool = False
1820
):
1921
# Handler defaults
2022
self.show_line: bool = show_line
2123
self.trace: bool = trace
2224
self.use_timestamp: bool = use_timestamp
2325
self.exit_script: bool = exit_script
24-
self.print_function: callable = print_function
26+
self.print_function: Callable = print_function
2527
self.return_string_rather_than_print: bool = return_string_rather_than_print
2628

2729
# Formatter defaults
2830
# # Color wise
29-
self.main_color: Colors = Colors.WHITE
30-
self.message_color: Colors = self.main_color
31-
self.trace_color: Colors = self.main_color
32-
self.timestamps_color: Colors = self.main_color
31+
self.main_color: Colors | str = Colors.WHITE
32+
self.message_color: Colors | str = self.main_color
33+
self.trace_color: Colors | str = self.main_color
34+
self.timestamps_color: Colors | str = self.main_color
3335
# # Format wise
34-
self.main_format: Format = Format.NORMAL
36+
self.main_format: Format | str = Format.NORMAL
3537
self.message_text_format: Format = self.main_format
3638
self.trace_text_format: Format = self.main_format
3739
self.timestamps_text_format: Format = self.main_format
3840
# # # Datetime wise
3941
self.datetime_format: str = "%Y-%m-%d %H:%M:%S"
4042

4143
def formatter(self,
42-
main_format: Format = None,
43-
timestamps_format: Format = None,
44-
trace_format: Format = None,
45-
message_format: Format = None,
46-
main_color: Colors = None,
47-
timestamps_color: Colors = None,
48-
trace_color: Colors = None,
49-
message_color: Colors = None,
44+
main_format: Format | str = None,
45+
timestamps_format: Format | str = None,
46+
trace_format: Format | str = None,
47+
message_format: Format | str = None,
48+
main_color: Colors | str = None,
49+
timestamps_color: Colors | str = None,
50+
trace_color: Colors | str = None,
51+
message_color: Colors | str = None,
5052
datetime_format: str = None
5153
):
5254
# Format wise
@@ -95,8 +97,9 @@ def exception(self,
9597
f"\n{self.trace_text_format}{self.trace_color}{trace_str}{Colors.RESET}"
9698
)
9799

98-
if (
99-
return_string_rather_than_print is None and self.return_string_rather_than_print) or return_string_rather_than_print:
100+
if ((
101+
return_string_rather_than_print is None and self.return_string_rather_than_print)
102+
or return_string_rather_than_print):
100103
return final_msg
101104
self.print_function(final_msg)
102105

TerminalPrint/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def __log_message(self, message, symbol: str, timestamp=None):
9292
log.write(f"[{symbol}] {message}\n")
9393

9494
# Log levels
95-
def info(self, message, log_to_file: bool = None, style: TPrintColors = None):
95+
def info(self, message, log_to_file: bool = None, style: TPrintColors | str = None):
9696
"""
9797
Logs an informational message.
9898
@@ -108,7 +108,7 @@ def info(self, message, log_to_file: bool = None, style: TPrintColors = None):
108108
if log_to_file:
109109
self.__log_message(message, "*", timestamp)
110110

111-
def warning(self, message, log_to_file: bool = None, style: TPrintColors = None):
111+
def warning(self, message, log_to_file: bool = None, style: TPrintColors | str = None):
112112
"""
113113
Logs a warning message.
114114
@@ -124,7 +124,7 @@ def warning(self, message, log_to_file: bool = None, style: TPrintColors = None)
124124
if log_to_file:
125125
self.__log_message(message, "!", timestamp)
126126

127-
def error(self, message, log_to_file: bool = None, style: TPrintColors = None):
127+
def error(self, message, log_to_file: bool = None, style: TPrintColors | str = None):
128128
"""
129129
Logs an error message.
130130
@@ -140,7 +140,7 @@ def error(self, message, log_to_file: bool = None, style: TPrintColors = None):
140140
if log_to_file:
141141
self.__log_message(message, "x", timestamp)
142142

143-
def debug(self, message, log_to_file: bool = None, style: TPrintColors = None):
143+
def debug(self, message, log_to_file: bool = None, style: TPrintColors | str = None):
144144
"""
145145
Logs a debug message if debug mode is enabled.
146146
@@ -157,7 +157,7 @@ def debug(self, message, log_to_file: bool = None, style: TPrintColors = None):
157157
if log_to_file:
158158
self.__log_message(message, "-", timestamp)
159159

160-
def critical(self, message, log_to_file: bool = None, style: TPrintColors = None):
160+
def critical(self, message, log_to_file: bool = None, style: TPrintColors | str = None):
161161
"""
162162
Logs a critical message.
163163
@@ -173,7 +173,7 @@ def critical(self, message, log_to_file: bool = None, style: TPrintColors = None
173173
if log_to_file:
174174
self.__log_message(message, "x", timestamp)
175175

176-
def success(self, message, log_to_file: bool = None, style: TPrintColors = None):
176+
def success(self, message, log_to_file: bool = None, style: TPrintColors | str = None):
177177
"""
178178
Logs a success message.
179179

0 commit comments

Comments
 (0)