feat(api): Integrate aggressive logging for all hook invocations
This commit is contained in:
@@ -40,6 +40,7 @@ _seq_lock = threading.Lock()
|
||||
|
||||
_comms_fh = None # file handle: logs/comms_<ts>.log
|
||||
_tool_fh = None # file handle: logs/toolcalls_<ts>.log
|
||||
_api_fh = None # file handle: logs/apihooks_<ts>.log
|
||||
|
||||
|
||||
def _now_ts() -> str:
|
||||
@@ -52,7 +53,7 @@ def open_session():
|
||||
opens the two log files for this session. Idempotent - a second call is
|
||||
ignored.
|
||||
"""
|
||||
global _ts, _comms_fh, _tool_fh, _seq
|
||||
global _ts, _comms_fh, _tool_fh, _api_fh, _seq
|
||||
|
||||
if _comms_fh is not None:
|
||||
return # already open
|
||||
@@ -65,6 +66,7 @@ def open_session():
|
||||
|
||||
_comms_fh = open(_LOG_DIR / f"comms_{_ts}.log", "w", encoding="utf-8", buffering=1)
|
||||
_tool_fh = open(_LOG_DIR / f"toolcalls_{_ts}.log", "w", encoding="utf-8", buffering=1)
|
||||
_api_fh = open(_LOG_DIR / f"apihooks_{_ts}.log", "w", encoding="utf-8", buffering=1)
|
||||
|
||||
_tool_fh.write(f"# Tool-call log — session {_ts}\n\n")
|
||||
_tool_fh.flush()
|
||||
@@ -72,13 +74,30 @@ def open_session():
|
||||
|
||||
def close_session():
|
||||
"""Flush and close both log files. Called on clean exit (optional)."""
|
||||
global _comms_fh, _tool_fh
|
||||
global _comms_fh, _tool_fh, _api_fh
|
||||
if _comms_fh:
|
||||
_comms_fh.close()
|
||||
_comms_fh = None
|
||||
if _tool_fh:
|
||||
_tool_fh.close()
|
||||
_tool_fh = None
|
||||
if _api_fh:
|
||||
_api_fh.close()
|
||||
_api_fh = None
|
||||
|
||||
|
||||
def log_api_hook(method: str, path: str, payload: str):
|
||||
"""
|
||||
Log an API hook invocation.
|
||||
"""
|
||||
if _api_fh is None:
|
||||
return
|
||||
ts_entry = datetime.datetime.now().strftime("%H:%M:%S")
|
||||
try:
|
||||
_api_fh.write(f"[{ts_entry}] {method} {path} - Payload: {payload}\n")
|
||||
_api_fh.flush()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def log_comms(entry: dict):
|
||||
|
||||
Reference in New Issue
Block a user