@@ -37,7 +37,7 @@ def usesTime(self):
3737 return self ._fmt .find (self .asctime_search ) >= 0
3838
3939 def format (self , record ):
40- return str ( self ._fmt ) % record .__dict__
40+ return self ._fmt % record .__dict__
4141
4242
4343class StrFormatStyle (PercentStyle ):
@@ -46,7 +46,7 @@ class StrFormatStyle(PercentStyle):
4646 asctime_search = '{asctime'
4747
4848 def format (self , record ):
49- return str ( self ._fmt ) .format (** record .__dict__ )
49+ return self ._fmt .format (** record .__dict__ )
5050
5151
5252class StringTemplateStyle (PercentStyle ):
@@ -58,7 +58,7 @@ def __init__(self, fmt):
5858 PercentStyle .__init__ (self , fmt )
5959 self ._tpl = {}
6060 for _ , v in fmt .items ():
61- self ._tpl [v ] = Template (str ( v ) )
61+ self ._tpl [v ] = Template (v )
6262
6363 def usesTime (self ):
6464 fmt = self ._fmt
@@ -220,6 +220,7 @@ def __init__(self, fmt=BASIC_FORMAT, datefmt=None, style='%', record_custom_attr
220220 self .cls = cls
221221 self .indent = indent
222222 self .separators = separators
223+ self .encoding = encoding
223224 self .default = default
224225 self .sort_keys = sort_keys
225226 self .kw = kw
@@ -262,10 +263,10 @@ def formatMessage(self, record):
262263 def format (self , record ):
263264 result = dictionary ()
264265
265- # store `record` origin attributes start
266+ # store `record` origin attributes, prevent other formatter use record error start
266267 _msg , _args = record .msg , record .args
267268 record .msg , record .args = '' , tuple ()
268- # store `record` origin attributes end
269+ # store `record` origin attributes, prevent other formatter use record error end
269270
270271 self .setRecordMessage (record , _msg , _args )
271272
@@ -274,6 +275,13 @@ def format(self, record):
274275 if self .record_custom_attrs :
275276 self .setRecordCustomAttrs (record )
276277
278+ # compatible python2 start
279+ if sys .version_info < (3 , 0 ):
280+ for k , v in record .__dict__ .items ():
281+ if isinstance (v , str ):
282+ record .__dict__ .update ({k : v .decode (self .encoding )})
283+ # compatible python2 end
284+
277285 for k , v in self .json_fmt .items ():
278286 # this is for keeping `record` attribute `type`
279287 if v in record .__dict__ :
@@ -284,8 +292,8 @@ def format(self, record):
284292 result [k ] = self .formatMessage (record )
285293 self ._style ._fmt = ''
286294
287- # apply `record` origin attributes start
295+ # apply `record` origin attributes, prevent other formatter use record error start
288296 record .msg , record .args = _msg , _args
289- # apply `record` origin attributes end
297+ # apply `record` origin attributes, prevent other formatter use record error end
290298
291299 return json .dumps (result , skipkeys = self .skipkeys , ensure_ascii = self .ensure_ascii , check_circular = self .check_circular , allow_nan = self .allow_nan , cls = self .cls , indent = self .indent , separators = self .separators , default = self .default , sort_keys = self .sort_keys , ** self .kw )
0 commit comments