7
7
"""
8
8
import logging
9
9
import time
10
+ import typing as t
11
+ from typing import overload
10
12
11
13
from colorama import Fore , Style
12
14
@@ -56,7 +58,21 @@ def set_style(
56
58
return prefix + message + suffix
57
59
58
60
59
- def default_log_template (self , record , stylized = False ):
61
+ class LogTemplateFn (t .Protocol ):
62
+ def __call__ (
63
+ self ,
64
+ record : logging .LogRecord ,
65
+ stylized : t .Optional [bool ],
66
+ ** kwargs : t .Any ,
67
+ ) -> str :
68
+ ...
69
+
70
+
71
+ def default_log_template (
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
)
@@ -104,7 +120,7 @@ def default_log_template(self, record, stylized=False):
104
120
105
121
106
122
class LogFormatter (logging .Formatter ):
107
- template = default_log_template
123
+ template : LogTemplateFn = default_log_template
108
124
109
125
def __init__ (self , color = True , * args , ** kwargs ):
110
126
logging .Formatter .__init__ (self , * args , ** kwargs )
@@ -125,7 +141,12 @@ 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
+ record : logging .LogRecord ,
146
+ stylized : t .Optional [bool ] = False ,
147
+ ** kwargs : t .Any ,
148
+ ) -> str :
149
+
129
150
"""
130
151
Return the prefix for the log message. Template for Formatter.
131
152
@@ -143,7 +164,7 @@ def debug_log_template(self, record):
143
164
144
165
reset = Style .RESET_ALL
145
166
levelname = (
146
- LEVEL_COLORS .get (record .levelname )
167
+ LEVEL_COLORS .get (record .levelname , "" )
147
168
+ Style .BRIGHT
148
169
+ "(%(levelname)1.1s)"
149
170
+ Style .RESET_ALL
@@ -188,4 +209,4 @@ def debug_log_template(self, record):
188
209
class DebugLogFormatter (LogFormatter ):
189
210
"""Provides greater technical details than standard log Formatter."""
190
211
191
- template = debug_log_template
212
+ template : LogTemplateFn = debug_log_template
0 commit comments