perf: replace json.dumps serialization test with isinstance fast path (#96) #109

Merged
cal merged 1 commits from ai/major-domo-v2-96 into next-release 2026-03-20 17:38:59 +00:00

View File

@ -24,6 +24,8 @@ JSONValue = Union[
str, int, float, bool, None, dict[str, Any], list[Any] # nested object # arrays
]
_SERIALIZABLE_TYPES = (str, int, float, bool, type(None))
class JSONFormatter(logging.Formatter):
"""Custom JSON formatter for structured file logging."""
@ -93,11 +95,11 @@ class JSONFormatter(logging.Formatter):
extra_data = {}
for key, value in record.__dict__.items():
if key not in excluded_keys:
# Ensure JSON serializable
try:
json.dumps(value)
if isinstance(value, _SERIALIZABLE_TYPES) or isinstance(
value, (list, dict)
):
extra_data[key] = value
except (TypeError, ValueError):
else:
extra_data[key] = str(value)
if extra_data: