@@ -13,11 +13,20 @@ class JsonFormatter(logging.Formatter):
13
13
14
14
def __init__ (
15
15
self ,
16
- fmt_dict : dict = None ,
16
+ fmt_dict : dict | None = None ,
17
17
time_format : str = "%Y-%m-%dT%H:%M:%S" ,
18
18
msec_format : str = "%s.%03dZ" ,
19
19
):
20
- self .fmt_dict = fmt_dict if fmt_dict is not None else {"message" : "message" }
20
+ self .fmt_dict = (
21
+ fmt_dict
22
+ if fmt_dict is not None
23
+ else {
24
+ "timestamp" : "asctime" ,
25
+ "level" : "levelname" ,
26
+ "logger" : "name" ,
27
+ "message" : "message" ,
28
+ }
29
+ )
21
30
self .default_time_format = time_format
22
31
self .default_msec_format = msec_format
23
32
self .datefmt = None
@@ -28,7 +37,10 @@ def usesTime(self) -> bool:
28
37
"""
29
38
return "asctime" in self .fmt_dict .values ()
30
39
31
- def formatMessage (self , record ) -> dict :
40
+ def formatMessage (self , record ) -> str :
41
+ raise NotImplementedError ()
42
+
43
+ def formatMessageDict (self , record ) -> dict :
32
44
"""
33
45
Return a dictionary of the relevant LogRecord attributes instead of a string.
34
46
KeyError is raised if an unknown attribute is provided in the fmt_dict.
@@ -45,7 +57,7 @@ def format(self, record) -> str:
45
57
if self .usesTime ():
46
58
record .asctime = self .formatTime (record , self .datefmt )
47
59
48
- message_dict = self .formatMessage (record )
60
+ message_dict = self .formatMessageDict (record )
49
61
50
62
if record .exc_info :
51
63
# Cache the traceback text to avoid converting it multiple times
0 commit comments