feat(logging): Integrate log pruning and auto-whitelisting into app lifecycle
This commit is contained in:
16
gui_2.py
16
gui_2.py
@@ -22,6 +22,8 @@ import numpy as np
|
||||
import api_hooks
|
||||
import mcp_client
|
||||
from performance_monitor import PerformanceMonitor
|
||||
from log_registry import LogRegistry
|
||||
from log_pruner import LogPruner
|
||||
|
||||
from fastapi import FastAPI, Depends, HTTPException, Security
|
||||
from fastapi.security.api_key import APIKeyHeader
|
||||
@@ -265,8 +267,22 @@ class App:
|
||||
|
||||
label = self.project.get("project", {}).get("name", "")
|
||||
session_logger.open_session(label=label)
|
||||
self._prune_old_logs()
|
||||
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
|
||||
def current_provider(self):
|
||||
return self._current_provider
|
||||
|
||||
@@ -86,8 +86,20 @@ def open_session(label: str | None = None):
|
||||
|
||||
|
||||
def close_session():
|
||||
"""Flush and close all log files. Called on clean exit (optional)."""
|
||||
global _comms_fh, _tool_fh, _api_fh, _cli_fh
|
||||
"""Flush and close all log files. Called on clean exit."""
|
||||
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:
|
||||
_comms_fh.close()
|
||||
_comms_fh = None
|
||||
@@ -97,7 +109,7 @@ def close_session():
|
||||
if _api_fh:
|
||||
_api_fh.close()
|
||||
_api_fh = None
|
||||
if _cli_fh: # Close the new log file handle
|
||||
if _cli_fh:
|
||||
_cli_fh.close()
|
||||
_cli_fh = None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user