@@ -53,8 +53,8 @@ def setup_logger(
53
53
def set_style (
54
54
message : str ,
55
55
stylized : bool ,
56
- style_before : t . Optional [ str ] = None ,
57
- style_after : t . Optional [ str ] = None ,
56
+ style_before : str = "" ,
57
+ style_after : str = "" ,
58
58
prefix : str = "" ,
59
59
suffix : str = "" ,
60
60
) -> str :
@@ -64,60 +64,56 @@ def set_style(
64
64
return prefix + message + suffix
65
65
66
66
67
- def default_log_template (
68
- self : t .Type [logging .Formatter ],
69
- record : logging .LogRecord ,
70
- stylized : t .Optional [bool ] = False ,
71
- ** kwargs : t .Any ,
72
- ) -> str :
73
- """
74
- Return the prefix for the log message. Template for Formatter.
75
-
76
- Parameters
77
- ----------
78
- :py:class:`logging.LogRecord` :
79
- object. this is passed in from inside the
80
- :py:meth:`logging.Formatter.format` record.
81
-
82
- Returns
83
- -------
84
- str
85
- template for logger message
86
- """
87
-
88
- reset = Style .RESET_ALL
89
- levelname = set_style (
90
- "(%(levelname)s)" ,
91
- stylized ,
92
- style_before = (LEVEL_COLORS .get (record .levelname , "" ) + Style .BRIGHT ),
93
- style_after = Style .RESET_ALL ,
94
- suffix = " " ,
95
- )
96
- asctime = set_style (
97
- "%(asctime)s" ,
98
- stylized ,
99
- style_before = (Fore .BLACK + Style .DIM + Style .BRIGHT ),
100
- style_after = (Fore .RESET + Style .RESET_ALL ),
101
- prefix = "[" ,
102
- suffix = "]" ,
103
- )
104
- name = set_style (
105
- "%(name)s" ,
106
- stylized ,
107
- style_before = (Fore .WHITE + Style .DIM + Style .BRIGHT ),
108
- style_after = (Fore .RESET + Style .RESET_ALL ),
109
- prefix = " " ,
110
- suffix = " " ,
111
- )
112
-
113
- if stylized :
114
- return reset + levelname + asctime + name + reset
115
-
116
- return levelname + asctime + name
117
-
118
-
119
67
class LogFormatter (logging .Formatter ):
120
- template = default_log_template
68
+ def template (
69
+ self : logging .Formatter ,
70
+ record : logging .LogRecord ,
71
+ stylized : bool = False ,
72
+ ** kwargs : t .Any ,
73
+ ) -> str :
74
+ """
75
+ Return the prefix for the log message. Template for Formatter.
76
+
77
+ Parameters
78
+ ----------
79
+ :py:class:`logging.LogRecord` :
80
+ object. this is passed in from inside the
81
+ :py:meth:`logging.Formatter.format` record.
82
+
83
+ Returns
84
+ -------
85
+ str
86
+ template for logger message
87
+ """
88
+ reset = Style .RESET_ALL
89
+ levelname = set_style (
90
+ "(%(levelname)s)" ,
91
+ stylized ,
92
+ style_before = (LEVEL_COLORS .get (record .levelname , "" ) + Style .BRIGHT ),
93
+ style_after = Style .RESET_ALL ,
94
+ suffix = " " ,
95
+ )
96
+ asctime = set_style (
97
+ "%(asctime)s" ,
98
+ stylized ,
99
+ style_before = (Fore .BLACK + Style .DIM + Style .BRIGHT ),
100
+ style_after = (Fore .RESET + Style .RESET_ALL ),
101
+ prefix = "[" ,
102
+ suffix = "]" ,
103
+ )
104
+ name = set_style (
105
+ "%(name)s" ,
106
+ stylized ,
107
+ style_before = (Fore .WHITE + Style .DIM + Style .BRIGHT ),
108
+ style_after = (Fore .RESET + Style .RESET_ALL ),
109
+ prefix = " " ,
110
+ suffix = " " ,
111
+ )
112
+
113
+ if stylized :
114
+ return reset + levelname + asctime + name + reset
115
+
116
+ return levelname + asctime + name
121
117
122
118
def __init__ (self , color : bool = True , * args , ** kwargs ) -> None :
123
119
logging .Formatter .__init__ (self , * args , ** kwargs )
@@ -129,7 +125,8 @@ def format(self, record: logging.LogRecord) -> str:
129
125
record .message = f"Bad message ({ e !r} ): { record .__dict__ !r} "
130
126
131
127
date_format = "%H:%m:%S"
132
- record .asctime = time .strftime (date_format , self .converter (record .created ))
128
+ formatting = self .converter (record .created ) # type:ignore
129
+ record .asctime = time .strftime (date_format , formatting )
133
130
134
131
prefix = self .template (record ) % record .__dict__
135
132
0 commit comments