28 lines
721 B
Python
28 lines
721 B
Python
"""审计日志:每次 MCP 调用记录一行(含只读)。"""
|
|
import json
|
|
import os
|
|
from datetime import datetime
|
|
|
|
_file = None
|
|
|
|
|
|
def init(path):
|
|
global _file
|
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
_file = open(path, "a", encoding="utf-8")
|
|
audit("system", "startup", "", "")
|
|
|
|
|
|
def audit(tool, serial, args_summary, result):
|
|
global _file
|
|
if _file is None:
|
|
return
|
|
rec = {"ts": datetime.now().isoformat(timespec="seconds"),
|
|
"tool": tool, "serial": serial,
|
|
"args": str(args_summary)[:200], "result": str(result)[:200]}
|
|
try:
|
|
_file.write(json.dumps(rec, ensure_ascii=False) + "\n")
|
|
_file.flush()
|
|
except Exception:
|
|
pass
|