Persist structured JSONL flow events alongside standard logs.
| 84 | |
| 85 | |
| 86 | class RuntimeLogService: |
| 87 | """Persist structured JSONL flow events alongside standard logs.""" |
| 88 | |
| 89 | def __init__(self, settings: Settings | None = None) -> None: |
| 90 | self.settings = settings or get_settings() |
| 91 | self._lock = threading.Lock() |
| 92 | self.logger = logging.getLogger("glm_desk.runtime") |
| 93 | configure_logging(self.settings) |
| 94 | (self.settings.runtime_logs_dir / "accounts").mkdir(parents=True, exist_ok=True) |
| 95 | |
| 96 | def start_run( |
| 97 | self, |
| 98 | *, |
| 99 | account_id: str, |
| 100 | action: str, |
| 101 | source: str = "", |
| 102 | product_id: str = "", |
| 103 | pay_type: str = "", |
| 104 | details: dict[str, Any] | None = None, |
| 105 | ) -> FlowRun: |
| 106 | run = FlowRun( |
| 107 | run_id=self._make_run_id(), |
| 108 | account_id=account_id, |
| 109 | action=action.strip() or "flow", |
| 110 | source=source.strip(), |
| 111 | product_id=product_id.strip(), |
| 112 | pay_type=pay_type.strip(), |
| 113 | ) |
| 114 | self.log_event( |
| 115 | run, |
| 116 | stage="run", |
| 117 | status="started", |
| 118 | message=f"{run.action} started", |
| 119 | details=details, |
| 120 | ) |
| 121 | return run |
| 122 | |
| 123 | def finish_run( |
| 124 | self, |
| 125 | run: FlowRun, |
| 126 | *, |
| 127 | status: str, |
| 128 | message: str, |
| 129 | details: dict[str, Any] | None = None, |
| 130 | level: int | None = None, |
| 131 | ) -> None: |
| 132 | resolved_level = level if level is not None else (logging.ERROR if status == "failed" else logging.INFO) |
| 133 | self.log_event( |
| 134 | run, |
| 135 | stage="run", |
| 136 | status=status, |
| 137 | message=message, |
| 138 | details=details, |
| 139 | level=resolved_level, |
| 140 | ) |
| 141 | |
| 142 | def log_event( |
| 143 | self, |
no outgoing calls
no test coverage detected