@@ -37,7 +37,7 @@ def usesTime(self):
37
37
return self ._fmt .find (self .asctime_search ) >= 0
38
38
39
39
def format (self , record ):
40
- return str ( self ._fmt ) % record .__dict__
40
+ return self ._fmt % record .__dict__
41
41
42
42
43
43
class StrFormatStyle (PercentStyle ):
@@ -46,7 +46,7 @@ class StrFormatStyle(PercentStyle):
46
46
asctime_search = '{asctime'
47
47
48
48
def format (self , record ):
49
- return str ( self ._fmt ) .format (** record .__dict__ )
49
+ return self ._fmt .format (** record .__dict__ )
50
50
51
51
52
52
class StringTemplateStyle (PercentStyle ):
@@ -58,7 +58,7 @@ def __init__(self, fmt):
58
58
PercentStyle .__init__ (self , fmt )
59
59
self ._tpl = {}
60
60
for _ , v in fmt .items ():
61
- self ._tpl [v ] = Template (str ( v ) )
61
+ self ._tpl [v ] = Template (v )
62
62
63
63
def usesTime (self ):
64
64
fmt = self ._fmt
@@ -220,6 +220,7 @@ def __init__(self, fmt=BASIC_FORMAT, datefmt=None, style='%', record_custom_attr
220
220
self .cls = cls
221
221
self .indent = indent
222
222
self .separators = separators
223
+ self .encoding = encoding
223
224
self .default = default
224
225
self .sort_keys = sort_keys
225
226
self .kw = kw
@@ -262,10 +263,10 @@ def formatMessage(self, record):
262
263
def format (self , record ):
263
264
result = dictionary ()
264
265
265
- # store `record` origin attributes start
266
+ # store `record` origin attributes, prevent other formatter use record error start
266
267
_msg , _args = record .msg , record .args
267
268
record .msg , record .args = '' , tuple ()
268
- # store `record` origin attributes end
269
+ # store `record` origin attributes, prevent other formatter use record error end
269
270
270
271
self .setRecordMessage (record , _msg , _args )
271
272
@@ -274,6 +275,13 @@ def format(self, record):
274
275
if self .record_custom_attrs :
275
276
self .setRecordCustomAttrs (record )
276
277
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
+
277
285
for k , v in self .json_fmt .items ():
278
286
# this is for keeping `record` attribute `type`
279
287
if v in record .__dict__ :
@@ -284,8 +292,8 @@ def format(self, record):
284
292
result [k ] = self .formatMessage (record )
285
293
self ._style ._fmt = ''
286
294
287
- # apply `record` origin attributes start
295
+ # apply `record` origin attributes, prevent other formatter use record error start
288
296
record .msg , record .args = _msg , _args
289
- # apply `record` origin attributes end
297
+ # apply `record` origin attributes, prevent other formatter use record error end
290
298
291
299
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