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: