2026-04-17 10:36:45 +01:00
|
|
|
"""Shared logging utilities for the Stirling AI engine."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
2026-04-23 14:19:27 +01:00
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
2026-04-17 10:36:45 +01:00
|
|
|
|
|
|
|
|
class Pretty:
|
|
|
|
|
"""Lazy JSON formatter — only serialises when ``str()`` is called.
|
|
|
|
|
|
|
|
|
|
Designed for use with ``logging``'s ``%s`` formatting so that the
|
|
|
|
|
JSON serialisation is skipped entirely when the log message is
|
2026-04-23 14:19:27 +01:00
|
|
|
never emitted. Pydantic models (at the top level or nested) are
|
|
|
|
|
dumped via ``model_dump``; anything else falls back to ``str``.
|
2026-04-17 10:36:45 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
__slots__ = ("_obj",)
|
|
|
|
|
|
|
|
|
|
def __init__(self, obj: object) -> None:
|
|
|
|
|
self._obj = obj
|
|
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2026-04-23 14:19:27 +01:00
|
|
|
if isinstance(self._obj, BaseModel):
|
|
|
|
|
return self._obj.model_dump_json(indent=2)
|
|
|
|
|
return json.dumps(self._obj, indent=2, default=_default, ensure_ascii=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _default(value: object) -> object:
|
|
|
|
|
if isinstance(value, BaseModel):
|
|
|
|
|
return value.model_dump()
|
|
|
|
|
return str(value)
|