from __future__ import annotations import json from pathlib import Path class RequestLog: def __init__(self, path): self.path = Path(path) def record(self, event): self.path.parent.mkdir(parents=True, exist_ok=True) with open(self.path, "a", encoding="utf-8") as fh: fh.write(json.dumps(event, sort_keys=True) + "\n") @classmethod def read(cls, path): p = Path(path) if not p.exists(): return [] out = [] with open(p, "r", encoding="utf-8") as fh: for line in fh: line = line.strip() if line: try: out.append(json.loads(line)) except ValueError: continue return out