Rajan Sharma commited on
Commit
386287a
·
verified ·
1 Parent(s): d0e3a07

Create audit_log.py

Browse files
Files changed (1) hide show
  1. audit_log.py +25 -0
audit_log.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json, time, hashlib
2
+ from typing import Any
3
+
4
+ def _hash(s: str) -> str:
5
+ return hashlib.sha256(s.encode("utf-8", errors="ignore")).hexdigest()
6
+
7
+ def log_event(event_type: str, user_id: str | None, meta: dict[str, Any]):
8
+ # Do NOT store content. Use hashes and sizes only.
9
+ rec = {
10
+ "ts": time.strftime("%Y-%m-%dT%H:%M:%S%z"),
11
+ "event": event_type,
12
+ "user": user_id or "unknown",
13
+ "meta": meta,
14
+ }
15
+ try:
16
+ with open("audit.jsonl", "a", encoding="utf-8") as f:
17
+ f.write(json.dumps(rec) + "\n")
18
+ except Exception:
19
+ pass
20
+
21
+ def hash_summary(label: str, text: str) -> dict:
22
+ return {
23
+ f"{label}_sha256": _hash(text),
24
+ f"{label}_chars": len(text or ""),
25
+ }