fix: prevent logging handler accumulation in gateway mode

Use exact Path comparison instead of endswith to detect existing
errors.log handlers, avoiding false positives from similarly-named
log files.
This commit is contained in:
Eris 2026-03-11 16:00:25 +08:00 committed by teknium1
parent a20d373945
commit c2a7921f3b

View file

@ -407,19 +407,30 @@ class AIAgent:
# Persistent error log -- always writes WARNING+ to ~/.hermes/logs/errors.log # Persistent error log -- always writes WARNING+ to ~/.hermes/logs/errors.log
# so tool failures, API errors, etc. are inspectable after the fact. # so tool failures, API errors, etc. are inspectable after the fact.
from agent.redact import RedactingFormatter # In gateway mode, each incoming message creates a new AIAgent instance,
_error_log_dir = _hermes_home / "logs" # while the root logger is process-global. Re-adding the same errors.log
_error_log_dir.mkdir(parents=True, exist_ok=True) # handler would cause each warning/error line to be written multiple times.
_error_log_path = _error_log_dir / "errors.log"
from logging.handlers import RotatingFileHandler from logging.handlers import RotatingFileHandler
_error_file_handler = RotatingFileHandler( root_logger = logging.getLogger()
_error_log_path, maxBytes=2 * 1024 * 1024, backupCount=2, error_log_dir = _hermes_home / "logs"
error_log_path = error_log_dir / "errors.log"
resolved_error_log_path = error_log_path.resolve()
has_errors_log_handler = any(
isinstance(handler, RotatingFileHandler)
and Path(getattr(handler, "baseFilename", "")).resolve() == resolved_error_log_path
for handler in root_logger.handlers
) )
_error_file_handler.setLevel(logging.WARNING) if not has_errors_log_handler:
_error_file_handler.setFormatter(RedactingFormatter( from agent.redact import RedactingFormatter
'%(asctime)s %(levelname)s %(name)s: %(message)s', error_log_dir.mkdir(parents=True, exist_ok=True)
)) error_file_handler = RotatingFileHandler(
logging.getLogger().addHandler(_error_file_handler) error_log_path, maxBytes=2 * 1024 * 1024, backupCount=2,
)
error_file_handler.setLevel(logging.WARNING)
error_file_handler.setFormatter(RedactingFormatter(
'%(asctime)s %(levelname)s %(name)s: %(message)s',
))
root_logger.addHandler(error_file_handler)
if self.verbose_logging: if self.verbose_logging:
logging.basicConfig( logging.basicConfig(