From 31a770848abe2684f0f98327861fabe20629b4e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Mart=C3=ADn?= Date: Tue, 4 Jun 2024 11:58:17 +0200 Subject: [PATCH] chore(CLI): add logging commands (#4114) --- cli/cli.py | 26 ++++++++++++++++++++++++++ test.log | 0 tests/cli/cli_test.py | 27 +++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 test.log diff --git a/cli/cli.py b/cli/cli.py index 54cc6f7c17..9988701623 100644 --- a/cli/cli.py +++ b/cli/cli.py @@ -16,6 +16,7 @@ from prowler.lib.check.check import ( print_services, ) from prowler.lib.check.checks_loader import load_checks_to_execute +from prowler.lib.logger import logger, logging_levels, set_logging_config app = typer.Typer() @@ -42,6 +43,13 @@ def check_compliance_framework(provider: str, compliance_framework: list): return valid_compliance_frameworks +def validate_log_level(log_level: str): + log_levels = list(logging_levels.keys()) + if log_level not in log_levels: + raise typer.BadParameter(f"Log level must be one of {log_levels}") + return log_level + + @app.command() def main( provider: str = typer.Argument(..., help="The provider to check"), @@ -72,6 +80,9 @@ def main( "--list-checks-json", help="List the checks of the provider in JSON format", ), + log_level: str = typer.Option("INFO", "--log-level", help="Set the Log level"), + log_file: str = typer.Option(None, "--log-file", help="Set the Log file"), + only_logs: bool = typer.Option(False, "--only-logs", help="Only show logs"), ): check_provider(provider) if list_services_bool: @@ -126,6 +137,21 @@ def main( provider, ) print(list_checks_json(provider, sorted(checks_to_execute))) + if log_level: + set_logging_config(validate_log_level(log_level)) + logger.info(f"Log level set to {log_level}") + if log_file: + if log_level: + set_logging_config(validate_log_level(log_level), log_file) + else: + set_logging_config("INFO", log_file) + logger.info(f"Log file set to {log_file}") + if only_logs: + if log_level: + set_logging_config(validate_log_level(log_level), only_logs=True) + else: + set_logging_config("INFO", only_logs=True) + logger.info("Only logs are shown") if __name__ == "__main__": diff --git a/test.log b/test.log new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/cli/cli_test.py b/tests/cli/cli_test.py index 5aefad132b..dce60acd25 100644 --- a/tests/cli/cli_test.py +++ b/tests/cli/cli_test.py @@ -66,3 +66,30 @@ class TestCLI: json.loads(result.output) except ValueError: assert False + + def test_log_level(self): + result = runner.invoke(app, ["aws", "--log-level", "ERROR"]) + assert result.exit_code == 0 + + def test_log_level_invalid(self): + result = runner.invoke(app, ["aws", "--log-level", "INVALID"]) + assert result.exit_code == 2 + assert "Log level must be one of" in result.output + + def test_log_level_no_value(self): + result = runner.invoke(app, ["aws", "--log-level"]) + assert result.exit_code == 2 + assert "Option '--log-level' requires an argument." in result.output + + def test_log_file(self): + result = runner.invoke(app, ["aws", "--log-file", "test.log"]) + assert result.exit_code == 0 + + def test_log_file_no_value(self): + result = runner.invoke(app, ["aws", "--log-file"]) + assert result.exit_code == 2 + assert "Option '--log-file' requires an argument." in result.output + + def test_only_logs(self): + result = runner.invoke(app, ["aws", "--only-logs"]) + assert result.exit_code == 0