feat(logging): Integrate log pruning and auto-whitelisting into app lifecycle

This commit is contained in:
2026-02-26 09:08:31 -05:00
parent 4e9c47f081
commit 8b7588323e
2 changed files with 31 additions and 3 deletions

View File

@@ -22,6 +22,8 @@ import numpy as np
import api_hooks import api_hooks
import mcp_client import mcp_client
from performance_monitor import PerformanceMonitor from performance_monitor import PerformanceMonitor
from log_registry import LogRegistry
from log_pruner import LogPruner
from fastapi import FastAPI, Depends, HTTPException, Security from fastapi import FastAPI, Depends, HTTPException, Security
from fastapi.security.api_key import APIKeyHeader from fastapi.security.api_key import APIKeyHeader
@@ -265,8 +267,22 @@ class App:
label = self.project.get("project", {}).get("name", "") label = self.project.get("project", {}).get("name", "")
session_logger.open_session(label=label) session_logger.open_session(label=label)
self._prune_old_logs()
self._init_ai_and_hooks() self._init_ai_and_hooks()
def _prune_old_logs(self):
"""Asynchronously prunes old insignificant logs on startup."""
def run_prune():
try:
registry = LogRegistry("logs/log_registry.toml")
pruner = LogPruner(registry, "logs")
pruner.prune()
except Exception as e:
print(f"Error during log pruning: {e}")
thread = threading.Thread(target=run_prune, daemon=True)
thread.start()
@property @property
def current_provider(self): def current_provider(self):
return self._current_provider return self._current_provider

View File

@@ -86,8 +86,20 @@ def open_session(label: str | None = None):
def close_session(): def close_session():
"""Flush and close all log files. Called on clean exit (optional).""" """Flush and close all log files. Called on clean exit."""
global _comms_fh, _tool_fh, _api_fh, _cli_fh global _comms_fh, _tool_fh, _api_fh, _cli_fh, _session_id, _LOG_DIR
if _comms_fh is None:
return
# Trigger auto-whitelist update for this session before closing
try:
from log_registry import LogRegistry
registry = LogRegistry(str(_LOG_DIR / "log_registry.toml"))
registry.update_auto_whitelist_status(_session_id)
except Exception as e:
print(f"Warning: Could not update auto-whitelist on close: {e}")
if _comms_fh: if _comms_fh:
_comms_fh.close() _comms_fh.close()
_comms_fh = None _comms_fh = None
@@ -97,7 +109,7 @@ def close_session():
if _api_fh: if _api_fh:
_api_fh.close() _api_fh.close()
_api_fh = None _api_fh = None
if _cli_fh: # Close the new log file handle if _cli_fh:
_cli_fh.close() _cli_fh.close()
_cli_fh = None _cli_fh = None