import uuid import json import os def make_session_id(): return str(uuid.uuid4()) _LOG_DIR = "/data/logs" def log_record(record): """Append-only: write a single JSON file to /data/logs (bucket-mounted storage). Never raises.""" try: os.makedirs(_LOG_DIR, exist_ok=True) session_id = record.get("session_id", "unknown") ts = record.get("timestamp", "ts") ts_safe = ts.replace(":", "-").replace(".", "-").replace("+", "") filename = os.path.join(_LOG_DIR, f"{session_id}_{ts_safe}.json") with open(filename, "w", encoding="utf-8") as f: json.dump(record, f, ensure_ascii=False) print(f"[logging] Saved {filename}") except Exception as e: print(f"[logging] Save failed (non-fatal): {e}")