Create log_utils.py
Browse files- src/log_utils.py +26 -0
src/log_utils.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# src/log_utils.py
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
from typing import Any, Dict
|
| 8 |
+
|
| 9 |
+
def _ensure_dir(path: str) -> None:
|
| 10 |
+
os.makedirs(path, exist_ok=True)
|
| 11 |
+
|
| 12 |
+
def write_audit_log(case_id: str, payload: Dict[str, Any], *, base_dir: str = "/data/econsult/audit") -> str:
|
| 13 |
+
"""Write a single JSON audit record per case."""
|
| 14 |
+
_ensure_dir(base_dir)
|
| 15 |
+
ts = datetime.utcnow().isoformat(timespec="seconds") + "Z"
|
| 16 |
+
record = dict(payload)
|
| 17 |
+
record["case_id"] = case_id
|
| 18 |
+
record["ts_iso"] = ts
|
| 19 |
+
# Normalize some floats for readability
|
| 20 |
+
for k in ("runtime_sec", "coverage_percent"):
|
| 21 |
+
if k in record and isinstance(record[k], (int, float)):
|
| 22 |
+
record[k] = float(f"{record[k]:.4f}")
|
| 23 |
+
fp = os.path.join(base_dir, f"{case_id}.json")
|
| 24 |
+
with open(fp, "w", encoding="utf-8") as f:
|
| 25 |
+
json.dump(record, f, indent=2, ensure_ascii=False)
|
| 26 |
+
return fp
|