fix(cli): configure logging before anything can log
This commit is contained in:
+5
-5
@@ -1449,6 +1449,11 @@ def cli(loglevel: str, group_path: str, exclude:Tuple[str]) -> None:
|
||||
global SETTINGS_PATH
|
||||
global GROUP_PATH
|
||||
global ABRA_DIR
|
||||
if loglevel:
|
||||
numeric_level = getattr(logging, loglevel.upper(), None)
|
||||
if not isinstance(numeric_level, int):
|
||||
raise ValueError('Invalid log level: %s' % loglevel)
|
||||
logging.basicConfig(level=numeric_level, force=True)
|
||||
SETTINGS_PATH = get_settings_path()
|
||||
preflight_configs([Path(SETTINGS_PATH).expanduser()])
|
||||
SETTINGS = read_config(SETTINGS_PATH)
|
||||
@@ -1474,11 +1479,6 @@ def cli(loglevel: str, group_path: str, exclude:Tuple[str]) -> None:
|
||||
INSTANCE_CONFIGS = merge_connection_configs(instance_configs)
|
||||
all_configs = get_merged_instance_configs(_root_path, all_group_configs, exclude_paths, config_sets)
|
||||
ALL_CONFIGS = merge_connection_configs(all_configs)
|
||||
if loglevel:
|
||||
numeric_level = getattr(logging, loglevel.upper(), None)
|
||||
if not isinstance(numeric_level, int):
|
||||
raise ValueError('Invalid log level: %s' % loglevel)
|
||||
logging.basicConfig(level=numeric_level)
|
||||
fetch_recipes(INSTANCE_CONFIGS)
|
||||
|
||||
|
||||
|
||||
@@ -169,3 +169,44 @@ class TestPreflightInCli:
|
||||
result = CliRunner().invoke(alakazam.cli, [str(config_root / "group"), "ls"])
|
||||
assert result.exit_code != 0
|
||||
assert "alakazam.yml" in result.output
|
||||
|
||||
|
||||
class TestLogLevel:
|
||||
"""-l has to win over the handler that an early logging.warning() installs by itself."""
|
||||
|
||||
@pytest.fixture
|
||||
def probe(self, config_root, monkeypatch):
|
||||
import logging
|
||||
monkeypatch.setattr(alakazam, "abra", lambda *a, **k: "")
|
||||
monkeypatch.setattr(alakazam, "fetch_recipes", lambda *a, **k: None)
|
||||
monkeypatch.setattr(alakazam, "get_settings_path", lambda: str(config_root / "alakazam.yml"))
|
||||
monkeypatch.setattr(alakazam, "get_abra_dir", lambda: config_root / ".abra")
|
||||
(config_root / "alakazam.yml").write_text(f"root: {config_root}\n")
|
||||
# a missing config-sets.yml makes read_config warn before cli() is through
|
||||
(config_root / "config-sets.yml").unlink()
|
||||
levels = []
|
||||
|
||||
@alakazam.cli.command()
|
||||
def probe():
|
||||
levels.append(logging.getLogger().getEffectiveLevel())
|
||||
|
||||
monkeypatch.setattr(alakazam, "GROUP_PATH", None)
|
||||
return levels
|
||||
|
||||
def run(self, config_root, level):
|
||||
return CliRunner().invoke(alakazam.cli, ["-l", level, str(config_root / "group"), "probe"])
|
||||
|
||||
def test_debug_reaches_the_root_logger(self, config_root, probe):
|
||||
import logging
|
||||
result = self.run(config_root, "DEBUG")
|
||||
assert result.exit_code == 0, result.output
|
||||
assert probe == [logging.DEBUG]
|
||||
|
||||
def test_info_reaches_the_root_logger(self, config_root, probe):
|
||||
import logging
|
||||
self.run(config_root, "INFO")
|
||||
assert probe == [logging.INFO]
|
||||
|
||||
def test_an_invalid_level_is_rejected(self, config_root, probe):
|
||||
result = self.run(config_root, "LOUD")
|
||||
assert result.exit_code != 0
|
||||
|
||||
Reference in New Issue
Block a user