7
7
"""
8
8
import logging
9
9
import time
10
+ import typing as t
10
11
11
12
from colorama import Fore , Style
12
13
@@ -56,7 +57,22 @@ def set_style(
56
57
return prefix + message + suffix
57
58
58
59
59
- def default_log_template (self , record , stylized = False ):
60
+ class LogTemplateFn (t .Protocol ):
61
+ def template (
62
+ self ,
63
+ record : logging .LogRecord ,
64
+ stylized : t .Optional [bool ],
65
+ ** kwargs : t .Any ,
66
+ ) -> str :
67
+ ...
68
+
69
+
70
+ def default_log_template (
71
+ self : t .Type [logging .Formatter ],
72
+ record : logging .LogRecord ,
73
+ stylized : t .Optional [bool ] = False ,
74
+ ** kwargs : t .Any ,
75
+ ) -> str :
60
76
"""
61
77
Return the prefix for the log message. Template for Formatter.
62
78
@@ -76,7 +92,7 @@ def default_log_template(self, record, stylized=False):
76
92
levelname = set_style (
77
93
"(%(levelname)s)" ,
78
94
stylized ,
79
- style_before = (LEVEL_COLORS .get (record .levelname ) + Style .BRIGHT ),
95
+ style_before = (LEVEL_COLORS .get (record .levelname , "" ) + Style .BRIGHT ),
80
96
style_after = Style .RESET_ALL ,
81
97
suffix = " " ,
82
98
)
@@ -103,7 +119,7 @@ def default_log_template(self, record, stylized=False):
103
119
return levelname + asctime + name
104
120
105
121
106
- class LogFormatter (logging .Formatter ):
122
+ class LogFormatter (logging .Formatter , LogTemplateFn ):
107
123
template = default_log_template
108
124
109
125
def __init__ (self , color = True , * args , ** kwargs ):
@@ -125,7 +141,13 @@ def format(self, record):
125
141
return formatted .replace ("\n " , "\n " + parts [0 ] + " " )
126
142
127
143
128
- def debug_log_template (self , record ):
144
+ def debug_log_template (
145
+ self : t .Type [logging .Formatter ],
146
+ record : logging .LogRecord ,
147
+ stylized : t .Optional [bool ] = False ,
148
+ ** kwargs : t .Any ,
149
+ ) -> str :
150
+
129
151
"""
130
152
Return the prefix for the log message. Template for Formatter.
131
153
@@ -143,7 +165,7 @@ def debug_log_template(self, record):
143
165
144
166
reset = Style .RESET_ALL
145
167
levelname = (
146
- LEVEL_COLORS .get (record .levelname )
168
+ LEVEL_COLORS .get (record .levelname , "" )
147
169
+ Style .BRIGHT
148
170
+ "(%(levelname)1.1s)"
149
171
+ Style .RESET_ALL
0 commit comments