Skip to content

Commit 1e24cb7

Browse files
authored
feat: Add --verbose/--quiet flags to cli (#1440)
Allows the logging to be configured from the CLI instead of requiring a configuration file. Verbose option increases logging to DEBUG level while quiet reduces it to ERROR only. If both options are passed, the second option overrides the first.
1 parent 56e5b2c commit 1e24cb7

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/blueapi/cli/cli.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,24 @@ def is_str_dict(val: Any) -> TypeGuard[TaskParameters]:
8383
@click.option(
8484
"-c", "--config", type=Path, help="Path to configuration YAML file", multiple=True
8585
)
86+
@click.option(
87+
"-v",
88+
"--verbose",
89+
"log_level",
90+
flag_value="DEBUG",
91+
help="Include DEBUG level logging output",
92+
)
93+
@click.option(
94+
"-q",
95+
"--quiet",
96+
"log_level",
97+
flag_value="ERROR",
98+
help="Reduce logging noise to only show errors",
99+
)
86100
@click.pass_context
87-
def main(ctx: click.Context, config: tuple[Path, ...]) -> None:
101+
def main(
102+
ctx: click.Context, config: tuple[Path, ...], log_level: str | None = None
103+
) -> None:
88104
# if no command is supplied, run with the options passed
89105

90106
# Set umask to DLS standard
@@ -96,6 +112,9 @@ def main(ctx: click.Context, config: tuple[Path, ...]) -> None:
96112
except FileNotFoundError as fnfe:
97113
raise ClickException(f"Config file not found: {fnfe.filename}") from fnfe
98114

115+
if log_level:
116+
config_loader.use_values({"logging": {"level": log_level}})
117+
99118
loaded_config: ApplicationConfig = config_loader.load()
100119

101120
set_up_logging(loaded_config.logging)

tests/unit_tests/cli/test_cli.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,3 +1384,14 @@ def test_config_schema(
13841384
def test_task_parameter_type(value, result):
13851385
t = ParametersType()
13861386
assert t.convert(value, None, None) == result
1387+
1388+
1389+
@pytest.mark.parametrize(
1390+
"flag,level",
1391+
[("--verbose", "DEBUG"), ("--quiet", "ERROR"), ("-v", "DEBUG"), ("-q", "ERROR")],
1392+
)
1393+
def test_log_level_override(flag: str, level: str, runner: CliRunner):
1394+
with patch("blueapi.log.logging") as mock_log:
1395+
runner.invoke(main, [flag])
1396+
mock_log.getLogger().setLevel.assert_called_once_with(level)
1397+
mock_log.StreamHandler().setLevel.assert_called_once_with(level)

0 commit comments

Comments
 (0)