77"""
88import logging
99import time
10+ import typing as t
1011
1112from colorama import Fore , Style
1213
@@ -56,7 +57,22 @@ def set_style(
5657 return prefix + message + suffix
5758
5859
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 :
6076 """
6177 Return the prefix for the log message. Template for Formatter.
6278
@@ -76,7 +92,7 @@ def default_log_template(self, record, stylized=False):
7692 levelname = set_style (
7793 "(%(levelname)s)" ,
7894 stylized ,
79- style_before = (LEVEL_COLORS .get (record .levelname ) + Style .BRIGHT ),
95+ style_before = (LEVEL_COLORS .get (record .levelname , "" ) + Style .BRIGHT ),
8096 style_after = Style .RESET_ALL ,
8197 suffix = " " ,
8298 )
@@ -103,7 +119,7 @@ def default_log_template(self, record, stylized=False):
103119 return levelname + asctime + name
104120
105121
106- class LogFormatter (logging .Formatter ):
122+ class LogFormatter (logging .Formatter , LogTemplateFn ):
107123 template = default_log_template
108124
109125 def __init__ (self , color = True , * args , ** kwargs ):
@@ -125,7 +141,13 @@ def format(self, record):
125141 return formatted .replace ("\n " , "\n " + parts [0 ] + " " )
126142
127143
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+
129151 """
130152 Return the prefix for the log message. Template for Formatter.
131153
@@ -143,7 +165,7 @@ def debug_log_template(self, record):
143165
144166 reset = Style .RESET_ALL
145167 levelname = (
146- LEVEL_COLORS .get (record .levelname )
168+ LEVEL_COLORS .get (record .levelname , "" )
147169 + Style .BRIGHT
148170 + "(%(levelname)1.1s)"
149171 + Style .RESET_ALL
0 commit comments