64
64
65
65
try :
66
66
# pylint: disable=deprecated-class
67
- from typing import Optional , Hashable
67
+ from typing import Optional , Hashable , Dict
68
68
from typing_extensions import Protocol
69
69
70
70
class WriteableStream (Protocol ):
@@ -156,23 +156,28 @@ class Formatter:
156
156
Only implements a sub-set of CPython logging.Formatter behavior,
157
157
but retains all the same arguments in order to match the API.
158
158
159
- The only init arguments currently supported are: fmt and defaults.
160
- All others are currently ignored
159
+ The only init arguments currently supported are: fmt, defaults and
160
+ style. All others are currently ignored
161
161
162
- The only style value currently supported is '{'. CPython has support
163
- for some others, but this implementation does not. Additionally, the
164
- default value for style in this implementation is '{' whereas the default
165
- style value in CPython is '%'
162
+ The only two styles currently supported are '%' and '{'. The default
163
+ style is '{'
166
164
"""
167
165
168
166
def __init__ ( # pylint: disable=too-many-arguments
169
- self , fmt = None , datefmt = None , style = "{" , validate = True , defaults = None
167
+ self ,
168
+ fmt : Optional [str ] = None ,
169
+ datefmt : Optional [str ] = None ,
170
+ style : str = "%" ,
171
+ validate : bool = True ,
172
+ defaults : Dict = None ,
170
173
):
171
174
self .fmt = fmt
172
175
self .datefmt = datefmt
173
176
self .style = style
174
- if self .style != "{" :
175
- raise ValueError ("Only '{' formatting sytle is supported at this time." )
177
+ if self .style not in ("{" , "%" ):
178
+ raise ValueError (
179
+ "Only '%' and '{' formatting style are supported at this time."
180
+ )
176
181
177
182
self .validate = validate
178
183
self .defaults = defaults
@@ -192,7 +197,7 @@ def format(self, record: LogRecord) -> str:
192
197
"created" : record .created ,
193
198
"args" : record .args ,
194
199
}
195
- if "{asctime}" in self .fmt :
200
+ if "{asctime}" in self .fmt or "%(asctime)s" in self . fmt :
196
201
now = time .localtime ()
197
202
# pylint: disable=line-too-long
198
203
vals [
@@ -203,6 +208,14 @@ def format(self, record: LogRecord) -> str:
203
208
for key , val in self .defaults .items ():
204
209
vals [key ] = val
205
210
211
+ if self .style not in ("{" , "%" ):
212
+ raise ValueError (
213
+ "Only '%' and '{' formatting style are supported at this time."
214
+ )
215
+
216
+ if self .style == "%" :
217
+ return self .fmt % vals
218
+
206
219
return self .fmt .format (** vals )
207
220
208
221
0 commit comments