6464
6565try :
6666 # pylint: disable=deprecated-class
67- from typing import Optional , Hashable
67+ from typing import Optional , Hashable , Dict
6868 from typing_extensions import Protocol
6969
7070 class WriteableStream (Protocol ):
@@ -156,23 +156,28 @@ class Formatter:
156156 Only implements a sub-set of CPython logging.Formatter behavior,
157157 but retains all the same arguments in order to match the API.
158158
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
161161
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 '{'
166164 """
167165
168166 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 ,
170173 ):
171174 self .fmt = fmt
172175 self .datefmt = datefmt
173176 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+ )
176181
177182 self .validate = validate
178183 self .defaults = defaults
@@ -192,7 +197,7 @@ def format(self, record: LogRecord) -> str:
192197 "created" : record .created ,
193198 "args" : record .args ,
194199 }
195- if "{asctime}" in self .fmt :
200+ if "{asctime}" in self .fmt or "%(asctime)s" in self . fmt :
196201 now = time .localtime ()
197202 # pylint: disable=line-too-long
198203 vals [
@@ -203,6 +208,14 @@ def format(self, record: LogRecord) -> str:
203208 for key , val in self .defaults .items ():
204209 vals [key ] = val
205210
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+
206219 return self .fmt .format (** vals )
207220
208221
0 commit comments