From f2a80cad913d03695db491e12772f6b124e7f882 Mon Sep 17 00:00:00 2001 From: David Duarte Date: Mon, 1 Aug 2022 21:51:19 +0200 Subject: Add a --verbose argument to the test_runner to set DEBUG logging level (#840) Co-authored-by: Ang Li --- mobly/logger.py | 17 +++++++++++++---- mobly/test_runner.py | 18 +++++++++++++++--- 2 files changed, 28 insertions(+), 7 deletions(-) (limited to 'mobly') diff --git a/mobly/logger.py b/mobly/logger.py index 200e299..27f747c 100644 --- a/mobly/logger.py +++ b/mobly/logger.py @@ -168,7 +168,7 @@ def get_log_file_timestamp(delta=None): return _get_timestamp('%m-%d-%Y_%H-%M-%S-%f', delta) -def _setup_test_logger(log_path, prefix=None): +def _setup_test_logger(log_path, console_level, prefix=None): """Customizes the root logger for a test run. The logger object has a stream handler and a file handler. The stream @@ -177,6 +177,9 @@ def _setup_test_logger(log_path, prefix=None): Args: log_path: Location of the log file. + console_level: Log level threshold used for log messages printed + to the console. Logs with a level less severe than + console_level will not be printed to the console. prefix: A prefix for each log line in terminal. filename: Name of the log file. The default is the time the logger is requested. @@ -192,7 +195,7 @@ def _setup_test_logger(log_path, prefix=None): c_formatter = logging.Formatter(terminal_format, log_line_time_format) ch = logging.StreamHandler(sys.stdout) ch.setFormatter(c_formatter) - ch.setLevel(logging.INFO) + ch.setLevel(console_level) # Log everything to file f_formatter = logging.Formatter(log_line_format, log_line_time_format) # Write logger output to files @@ -237,7 +240,10 @@ def create_latest_log_alias(actual_path, alias): utils.create_alias(actual_path, alias_path) -def setup_test_logger(log_path, prefix=None, alias='latest'): +def setup_test_logger(log_path, + prefix=None, + alias='latest', + console_level=logging.INFO): """Customizes the root logger for a test run. In addition to configuring the Mobly logging handlers, this also sets two @@ -256,9 +262,12 @@ def setup_test_logger(log_path, prefix=None, alias='latest'): will not be created, which is useful to save storage space when the storage system (e.g. ZIP files) does not properly support shortcut/symlinks. + console_level: optional logging level, log level threshold used for log + messages printed to the console. Logs with a level less severe than + console_level will not be printed to the console. """ utils.create_dir(log_path) - _setup_test_logger(log_path, prefix) + _setup_test_logger(log_path, console_level, prefix) logging.debug('Test output folder: "%s"', log_path) if alias: create_latest_log_alias(log_path, alias=alias) diff --git a/mobly/test_runner.py b/mobly/test_runner.py index 5324bf3..b618d52 100644 --- a/mobly/test_runner.py +++ b/mobly/test_runner.py @@ -65,12 +65,13 @@ def main(argv=None): tests = None if args.tests: tests = args.tests + console_level = logging.DEBUG if args.verbose else logging.INFO # Execute the test class with configs. ok = True for config in test_configs: runner = TestRunner(log_dir=config.log_path, testbed_name=config.testbed_name) - with runner.mobly_logger(): + with runner.mobly_logger(console_level=console_level): runner.add_test_class(config, test_class, tests) try: runner.run() @@ -125,6 +126,11 @@ def parse_mobly_cli_args(argv): type=str, metavar='[ ...]', help='Specify which test beds to run tests on.') + + parser.add_argument('-v', + '--verbose', + action='store_true', + help='Set console logger level to DEBUG') if not argv: argv = sys.argv[1:] return parser.parse_known_args(argv)[0] @@ -289,20 +295,26 @@ class TestRunner: self._test_run_metadata = TestRunner._TestRunMetaData(log_dir, testbed_name) @contextlib.contextmanager - def mobly_logger(self, alias='latest'): + def mobly_logger(self, alias='latest', console_level=logging.INFO): """Starts and stops a logging context for a Mobly test run. Args: alias: optional string, the name of the latest log alias directory to create. If a falsy value is specified, then the directory will not be created. + console_level: optional logging level, log level threshold used for log + messages printed to the console. Logs with a level less severe than + console_level will not be printed to the console. Yields: The host file path where the logs for the test run are stored. """ # Refresh the log path at the beginning of the logger context. root_output_path = self._test_run_metadata.generate_test_run_log_path() - logger.setup_test_logger(root_output_path, self._testbed_name, alias=alias) + logger.setup_test_logger(root_output_path, + self._testbed_name, + alias=alias, + console_level=console_level) try: yield self._test_run_metadata.root_output_path finally: -- cgit v1.2.3