diff --git a/hermes_logging.py b/hermes_logging.py index 0ebc450a22..8d16e653c7 100644 --- a/hermes_logging.py +++ b/hermes_logging.py @@ -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) diff --git a/tests/test_hermes_logging.py b/tests/test_hermes_logging.py index 586a4d6666..c4168f79b9 100644 --- a/tests/test_hermes_logging.py +++ b/tests/test_hermes_logging.py @@ -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")