feat(api): Integrate aggressive logging for all hook invocations
This commit is contained in:
@@ -2,6 +2,7 @@ import json
|
||||
import threading
|
||||
from http.server import HTTPServer, BaseHTTPRequestHandler
|
||||
import logging
|
||||
import session_logger
|
||||
|
||||
class HookServerInstance(HTTPServer):
|
||||
def __init__(self, server_address, RequestHandlerClass, app):
|
||||
@@ -11,6 +12,7 @@ class HookServerInstance(HTTPServer):
|
||||
class HookHandler(BaseHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
app = self.server.app
|
||||
session_logger.log_api_hook("GET", self.path, "")
|
||||
if self.path == '/status':
|
||||
self.send_response(200)
|
||||
self.send_header('Content-Type', 'application/json')
|
||||
@@ -34,9 +36,11 @@ class HookHandler(BaseHTTPRequestHandler):
|
||||
app = self.server.app
|
||||
content_length = int(self.headers.get('Content-Length', 0))
|
||||
body = self.rfile.read(content_length)
|
||||
body_str = body.decode('utf-8') if body else ""
|
||||
session_logger.log_api_hook("POST", self.path, body_str)
|
||||
|
||||
try:
|
||||
data = json.loads(body.decode('utf-8')) if body else {}
|
||||
data = json.loads(body_str) if body_str else {}
|
||||
if self.path == '/api/project':
|
||||
app.project = data.get('project', app.project)
|
||||
self.send_response(200)
|
||||
|
||||
@@ -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