feat: Improved default fmt_dict for json formatter

This commit is contained in:
Matthias
2025-03-16 09:54:21 +01:00
parent 7db62689c4
commit 24e94cfaa7

View File

@@ -13,11 +13,20 @@ class JsonFormatter(logging.Formatter):
def __init__( def __init__(
self, self,
fmt_dict: dict = None, fmt_dict: dict | None = None,
time_format: str = "%Y-%m-%dT%H:%M:%S", time_format: str = "%Y-%m-%dT%H:%M:%S",
msec_format: str = "%s.%03dZ", msec_format: str = "%s.%03dZ",
): ):
self.fmt_dict = fmt_dict if fmt_dict is not None else {"message": "message"} self.fmt_dict = (
fmt_dict
if fmt_dict is not None
else {
"timestamp": "asctime",
"level": "levelname",
"logger": "name",
"message": "message",
}
)
self.default_time_format = time_format self.default_time_format = time_format
self.default_msec_format = msec_format self.default_msec_format = msec_format
self.datefmt = None self.datefmt = None
@@ -28,7 +37,10 @@ class JsonFormatter(logging.Formatter):
""" """
return "asctime" in self.fmt_dict.values() return "asctime" in self.fmt_dict.values()
def formatMessage(self, record) -> dict: def formatMessage(self, record) -> str:
raise NotImplementedError()
def formatMessageDict(self, record) -> dict:
""" """
Return a dictionary of the relevant LogRecord attributes instead of a string. Return a dictionary of the relevant LogRecord attributes instead of a string.
KeyError is raised if an unknown attribute is provided in the fmt_dict. KeyError is raised if an unknown attribute is provided in the fmt_dict.
@@ -45,7 +57,7 @@ class JsonFormatter(logging.Formatter):
if self.usesTime(): if self.usesTime():
record.asctime = self.formatTime(record, self.datefmt) record.asctime = self.formatTime(record, self.datefmt)
message_dict = self.formatMessage(record) message_dict = self.formatMessageDict(record)
if record.exc_info: if record.exc_info:
# Cache the traceback text to avoid converting it multiple times # Cache the traceback text to avoid converting it multiple times