From 70c4555a746ff1fd9b2c1d18ccf609578eb4e421 Mon Sep 17 00:00:00 2001 From: Cal Corum Date: Fri, 20 Mar 2026 12:32:34 -0500 Subject: [PATCH] perf: replace json.dumps serialization test with isinstance fast path (#96) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- utils/logging.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/utils/logging.py b/utils/logging.py index 92c0f05..6c6dfde 100644 --- a/utils/logging.py +++ b/utils/logging.py @@ -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: -- 2.25.1