3
3
from typing import Any
4
4
5
5
6
- def json_formatter (unserialized_value : Any ):
7
- """JSON custom serializer to cast unserialisable values to strings.
6
+ def json_formatter (unserializable_value : Any ):
7
+ """JSON custom serializer to cast unserializable values to strings.
8
8
9
9
Example
10
10
-------
11
11
12
- **Serialize unserialisable value to string**
12
+ **Serialize unserializable value to string**
13
13
14
14
class X: pass
15
15
value = {"x": X()}
@@ -18,10 +18,10 @@ class X: pass
18
18
19
19
Parameters
20
20
----------
21
- unserialized_value : Any
21
+ unserializable_value : Any
22
22
Python object unserializable by JSON
23
23
"""
24
- return str (unserialized_value )
24
+ return str (unserializable_value )
25
25
26
26
27
27
class JsonFormatter (logging .Formatter ):
@@ -39,11 +39,12 @@ def __init__(self, **kwargs):
39
39
"""Return a JsonFormatter instance.
40
40
41
41
The `json_default` kwarg is used to specify a formatter for otherwise
42
- unserialisable values. It must not throw. Defaults to a function that
42
+ unserializable values. It must not throw. Defaults to a function that
43
43
coerces the value to a string.
44
44
45
45
Other kwargs are used to specify log field format strings.
46
46
"""
47
+ self .default_json_formatter = kwargs .pop ("json_default" , json_formatter )
47
48
datefmt = kwargs .pop ("datefmt" , None )
48
49
49
50
super (JsonFormatter , self ).__init__ (datefmt = datefmt )
@@ -54,7 +55,6 @@ def __init__(self, **kwargs):
54
55
"location" : "%(funcName)s:%(lineno)d" ,
55
56
}
56
57
self .format_dict .update (kwargs )
57
- self .default_json_formatter = kwargs .pop ("json_default" , json_formatter )
58
58
59
59
def update_formatter (self , ** kwargs ):
60
60
self .format_dict .update (kwargs )
@@ -64,6 +64,7 @@ def format(self, record): # noqa: A003
64
64
record_dict ["asctime" ] = self .formatTime (record , self .datefmt )
65
65
66
66
log_dict = {}
67
+
67
68
for key , value in self .format_dict .items ():
68
69
if value and key in self .reserved_keys :
69
70
# converts default logging expr to its record value
@@ -84,19 +85,13 @@ def format(self, record): # noqa: A003
84
85
except (json .decoder .JSONDecodeError , TypeError , ValueError ):
85
86
pass
86
87
87
- if record .exc_info :
88
+ if record .exc_info and not record . exc_text :
88
89
# Cache the traceback text to avoid converting it multiple times
89
90
# (it's constant anyway)
90
91
# from logging.Formatter:format
91
- if not record .exc_text : # pragma: no cover
92
- record .exc_text = self .formatException (record .exc_info )
92
+ record .exc_text = self .formatException (record .exc_info )
93
93
94
94
if record .exc_text :
95
95
log_dict ["exception" ] = record .exc_text
96
96
97
- json_record = json .dumps (log_dict , default = self .default_json_formatter )
98
-
99
- if hasattr (json_record , "decode" ): # pragma: no cover
100
- json_record = json_record .decode ("utf-8" )
101
-
102
- return json_record
97
+ return json .dumps (log_dict , default = self .default_json_formatter )
0 commit comments