fix(logging): attach gateway log after cli init

This commit is contained in:
helix4u
2026-04-26 15:17:06 -06:00
committed by Teknium
parent cebf95854b
commit 88a85d30c1
2 changed files with 39 additions and 4 deletions

View File

@@ -195,10 +195,6 @@ def setup_logging(
The ``logs/`` directory where files are written.
"""
global _logging_initialized
if _logging_initialized and not force:
home = hermes_home or get_hermes_home()
return home / "logs"
home = hermes_home or get_hermes_home()
log_dir = home / "logs"
log_dir.mkdir(parents=True, exist_ok=True)
@@ -248,6 +244,9 @@ def setup_logging(
log_filter=_ComponentFilter(COMPONENT_PREFIXES["gateway"]),
)
if _logging_initialized and not force:
return log_dir
# Ensure root logger level is low enough for the handlers to fire.
if root.level == logging.NOTSET or root.level > level:
root.setLevel(level)

View File

@@ -261,6 +261,42 @@ class TestGatewayMode:
]
assert len(gw_handlers) == 0
def test_gateway_log_created_after_cli_init(self, hermes_home):
"""Gateway mode attaches gateway.log even after earlier CLI init."""
hermes_logging.setup_logging(hermes_home=hermes_home, mode="cli")
hermes_logging.setup_logging(hermes_home=hermes_home, mode="gateway")
root = logging.getLogger()
gw_handlers = [
h for h in root.handlers
if isinstance(h, RotatingFileHandler)
and "gateway.log" in getattr(h, "baseFilename", "")
]
assert len(gw_handlers) == 1
logging.getLogger("gateway.run").info("gateway connected after cli init")
for h in root.handlers:
h.flush()
gw_log = hermes_home / "logs" / "gateway.log"
assert gw_log.exists()
assert "gateway connected after cli init" in gw_log.read_text()
def test_gateway_log_created_after_cli_init_without_duplicate_handlers(self, hermes_home):
"""Repeated gateway setup calls do not attach duplicate gateway handlers."""
hermes_logging.setup_logging(hermes_home=hermes_home, mode="cli")
hermes_logging.setup_logging(hermes_home=hermes_home, mode="gateway")
hermes_logging.setup_logging(hermes_home=hermes_home, mode="gateway")
root = logging.getLogger()
gw_handlers = [
h for h in root.handlers
if isinstance(h, RotatingFileHandler)
and "gateway.log" in getattr(h, "baseFilename", "")
]
assert len(gw_handlers) == 1
def test_gateway_log_receives_gateway_records(self, hermes_home):
"""gateway.log captures records from gateway.* loggers."""
hermes_logging.setup_logging(hermes_home=hermes_home, mode="gateway")