feat(logging): Implement session-based log organization
This commit is contained in:
@@ -26,46 +26,62 @@ _LOG_DIR = Path("./logs")
|
||||
_SCRIPTS_DIR = Path("./scripts/generated")
|
||||
|
||||
_ts: str = "" # session timestamp string e.g. "20260301_142233"
|
||||
_session_id: str = "" # YYYYMMDD_HHMMSS[_Label]
|
||||
_session_dir: Path = None # Path to the sub-directory for this session
|
||||
_seq: int = 0 # monotonic counter for script files this session
|
||||
_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 - API hook calls
|
||||
_cli_fh = None # file handle: logs/clicalls_<ts>.log - CLI subprocess calls
|
||||
_comms_fh = None # file handle: logs/<session_id>/comms.log
|
||||
_tool_fh = None # file handle: logs/<session_id>/toolcalls.log
|
||||
_api_fh = None # file handle: logs/<session_id>/apihooks.log
|
||||
_cli_fh = None # file handle: logs/<session_id>/clicalls.log
|
||||
|
||||
|
||||
def _now_ts() -> str:
|
||||
return datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
|
||||
|
||||
def open_session():
|
||||
def open_session(label: str | None = None):
|
||||
"""
|
||||
Called once at GUI startup. Creates the log directories if needed and
|
||||
opens the two log files for this session. Idempotent - a second call is
|
||||
ignored.
|
||||
opens the log files for this session within a sub-directory.
|
||||
"""
|
||||
global _ts, _comms_fh, _tool_fh, _api_fh, _cli_fh, _seq
|
||||
global _ts, _session_id, _session_dir, _comms_fh, _tool_fh, _api_fh, _cli_fh, _seq
|
||||
|
||||
if _comms_fh is not None:
|
||||
return # already open
|
||||
|
||||
_LOG_DIR.mkdir(parents=True, exist_ok=True)
|
||||
_ts = _now_ts()
|
||||
_session_id = _ts
|
||||
if label:
|
||||
# Sanitize label: remove non-alphanumeric chars
|
||||
safe_label = "".join(c if c.isalnum() or c in ("-", "_") else "_" for c in label)
|
||||
_session_id += f"_{safe_label}"
|
||||
|
||||
_session_dir = _LOG_DIR / _session_id
|
||||
_session_dir.mkdir(parents=True, exist_ok=True)
|
||||
_SCRIPTS_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
_ts = _now_ts()
|
||||
_seq = 0
|
||||
|
||||
_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)
|
||||
_cli_fh = open(_LOG_DIR / f"clicalls_{_ts}.log", "w", encoding="utf-8", buffering=1) # New log file handle
|
||||
_comms_fh = open(_session_dir / "comms.log", "w", encoding="utf-8", buffering=1)
|
||||
_tool_fh = open(_session_dir / "toolcalls.log", "w", encoding="utf-8", buffering=1)
|
||||
_api_fh = open(_session_dir / "apihooks.log", "w", encoding="utf-8", buffering=1)
|
||||
_cli_fh = open(_session_dir / "clicalls.log", "w", encoding="utf-8", buffering=1)
|
||||
|
||||
_tool_fh.write(f"# Tool-call log — session {_ts}\n\n")
|
||||
_tool_fh.write(f"# Tool-call log — session {_session_id}\n\n")
|
||||
_tool_fh.flush()
|
||||
_cli_fh.write(f"# CLI Subprocess Call Log — session {_ts}\n\n") # Header for new log file
|
||||
_cli_fh.write(f"# CLI Subprocess Call Log — session {_session_id}\n\n")
|
||||
_cli_fh.flush()
|
||||
|
||||
# Register this session in the log registry
|
||||
try:
|
||||
from log_registry import LogRegistry
|
||||
registry = LogRegistry(str(_LOG_DIR / "log_registry.toml"))
|
||||
registry.register_session(_session_id, str(_session_dir), datetime.datetime.now())
|
||||
except Exception as e:
|
||||
print(f"Warning: Could not register session in LogRegistry: {e}")
|
||||
|
||||
atexit.register(close_session)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user