perf: replace json.dumps serialization test with isinstance fast path (#96)
All checks were successful
Build Docker Image / build (pull_request) Successful in 1m22s

Closes #96

Replaces the per-field `json.dumps(value)` probe — which fully serialized
and discarded the result just to check serializability — with a type-check
fast path using `isinstance()`. The `_SERIALIZABLE_TYPES` tuple is defined
at module level so it's not recreated on every log call.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Cal Corum 2026-03-20 12:32:34 -05:00
parent 6c49233392
commit 70c4555a74

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: