Skip to content

Instantly share code, notes, and snippets.

@colonelpanic8
Created August 8, 2024 23:56
Show Gist options
  • Save colonelpanic8/20a6a1ee841d64677ee38e8bcf46bd7e to your computer and use it in GitHub Desktop.
Save colonelpanic8/20a6a1ee841d64677ee38e8bcf46bd7e to your computer and use it in GitHub Desktop.
class StructLogCloudLoggingHandler(google_logging_v2.handlers.CloudLoggingHandler):
_allowed_types = (dict, list, bool, str, int, float)
_max_depth = 3
def _make_suitable_for_pb_conversion(self, dict_value, depth=0):
if depth > self._max_depth:
return
for key in list(dict_value.keys()):
value = dict_value[key]
if isinstance(value, tuple):
value[key] = list(value)
elif isinstance(value, enum.Enum):
value[key] = str(value)
elif isinstance(value, dict):
self._make_suitable_for_pb_conversion(value, depth=depth + 1)
elif not isinstance(value, self._allowed_types):
try:
value[key] = str(value)
except Exception:
del value[key]
def emit(self, record):
# import ipdb
# ipdb.set_trace()
# self.format
# if isinstance(record.msg, collections.abc.Mapping):
# self._make_suitable_for_pb_conversion(record.msg)
super().emit(record)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment