diag: Add support for custom json formatter in JsonElement
The JsonElement class is primarily used for structured logging, as it provides a means to format objs as json via printf style formatting outside the "hot" path.
This commit is contained in:
1 parent
e01a825aff
commit
6448b4cf90
1 file changed
+11
-2
@@ -30,7 +30,12 @@ class JsonElement:
|
||||
self.value = value
|
||||
|
||||
def __str__(self):
|
||||
return json.dumps(self.value)
|
||||
try:
|
||||
to_json = getattr(self.value, "to_json", None)
|
||||
return json.dumps(to_json(self.value) if callable(to_json) else self.value)
|
||||
except Exception as e:
|
||||
print(f"JsonElement formatting error: {e}")
|
||||
return str(self.value)
|
||||
|
||||
|
||||
class StructuredLog:
|
||||
@@ -40,7 +45,11 @@ class StructuredLog:
|
||||
kwargs["ts"] = time.time_ns()
|
||||
|
||||
def __str__(self):
|
||||
return json.dumps(self.kwargs)
|
||||
try:
|
||||
return json.dumps(self.kwargs)
|
||||
except Exception as e:
|
||||
print(f"StructuredLog formatting error: {e}")
|
||||
return str(self.kwargs)
|
||||
|
||||
|
||||
def _log_level_name(level: int):
|
||||
|
||||
Reference in new issue
Block a user