aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Duarte <git@delta.sh>2022-08-01 21:51:19 +0200
committerGitHub <noreply@github.com>2022-08-01 12:51:19 -0700
commitf2a80cad913d03695db491e12772f6b124e7f882 (patch)
tree788b11cf52dae197683bdae96ed91882fe0cbb88
parent04bef05b571e6dc8679b5beddb383038883f80b2 (diff)
downloadmobly-f2a80cad913d03695db491e12772f6b124e7f882.tar.gz
Add a --verbose argument to the test_runner to set DEBUG logging level (#840)
Co-authored-by: Ang Li <angli@google.com>
-rw-r--r--mobly/logger.py17
-rw-r--r--mobly/test_runner.py18
-rwxr-xr-xtests/mobly/logger_test.py4
3 files changed, 31 insertions, 8 deletions
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='[<TEST BED NAME1> <TEST BED NAME2> ...]',
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:
diff --git a/tests/mobly/logger_test.py b/tests/mobly/logger_test.py
index fa514e7..1d568c5 100755
--- a/tests/mobly/logger_test.py
+++ b/tests/mobly/logger_test.py
@@ -16,6 +16,7 @@ import os
import shutil
import tempfile
import unittest
+import logging
from unittest import mock
from mobly import logger
@@ -56,7 +57,8 @@ class LoggerTest(unittest.TestCase):
mock_create_latest_log_alias,
mock__setup_test_logger):
logger.setup_test_logger(self.log_dir)
- mock__setup_test_logger.assert_called_once_with(self.log_dir, None)
+ mock__setup_test_logger.assert_called_once_with(self.log_dir, logging.INFO,
+ None)
mock_create_latest_log_alias.assert_called_once_with(self.log_dir,
alias='latest')