Bug 1421799 - [test] Add mozlog logging arguments to |mach test| and |mach mochitest| r=gbrown
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Wed, 06 Dec 2017 17:29:23 -0500
changeset 449441 9b58dffaaa49d3fd249d7f69b9aca6da77371a07
parent 449440 cee49b47add020baa5548906c93e10937b73611b
child 449442 f8b4a929c55cb7b7107596bdcc04dc2b5e002994
push id8527
push userCallek@gmail.com
push dateThu, 11 Jan 2018 21:05:50 +0000
treeherdermozilla-beta@95342d212a7a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgbrown
bugs1421799
milestone59.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1421799 - [test] Add mozlog logging arguments to |mach test| and |mach mochitest| r=gbrown MozReview-Commit-ID: CaysVHzNTWo
testing/mach_commands.py
testing/mochitest/mach_commands.py
--- a/testing/mach_commands.py
+++ b/testing/mach_commands.py
@@ -64,26 +64,34 @@ class TestConfig(object):
         level_desc = "The default log level to use when running tests with `mach test`."
         level_choices = [l.lower() for l in log_levels]
         return [
             ('test.format', 'string', format_desc, 'tbpl', {'choices': format_choices}),
             ('test.level', 'string', level_desc, 'info', {'choices': level_choices}),
         ]
 
 
+def get_test_parser():
+    from mozlog.commandline import add_logging_group
+    parser = argparse.ArgumentParser()
+    parser.add_argument('what', default=None, nargs='*', help=TEST_HELP)
+    parser.add_argument('extra_args', default=None, nargs=argparse.REMAINDER,
+                        help="Extra arguments to pass to the underlying test command(s). "
+                             "If an underlying command doesn't recognize the argument, it "
+                             "will fail.")
+    add_logging_group(parser)
+    return parser
+
+
 @CommandProvider
 class Test(MachCommandBase):
     @Command('test', category='testing',
-             description='Run tests (detects the kind of test and runs it).')
-    @CommandArgument('what', default=None, nargs='*', help=TEST_HELP)
-    @CommandArgument('extra_args', default=None, nargs=argparse.REMAINDER,
-                     help="Extra arguments to pass to the underlying test command(s). "
-                          "If an underlying command doesn't recognize the argument, it "
-                          "will fail.")
-    def test(self, what, extra_args):
+             description='Run tests (detects the kind of test and runs it).',
+             parser=get_test_parser)
+    def test(self, what, extra_args, **log_args):
         """Run tests from names or paths.
 
         mach test accepts arguments specifying which tests to run. Each argument
         can be:
 
         * The path to a test file
         * A directory containing tests
         * A test suite name
@@ -97,35 +105,32 @@ class Test(MachCommandBase):
         When paths or directories are given, they are first resolved to test
         files known to the build system.
 
         If resolved tests belong to more than one test type/flavor/harness,
         the harness for each relevant type/flavor will be invoked. e.g. if
         you specify a directory with xpcshell and browser chrome mochitests,
         both harnesses will be invoked.
         """
-        from mozlog.commandline import log_formatters
-        from mozlog.handlers import StreamHandler, LogLevelFilter
-        from mozlog.structuredlog import StructuredLogger
+        from mozlog.commandline import setup_logging
         from moztest.resolve import TestResolver, TEST_FLAVORS, TEST_SUITES
 
         resolver = self._spawn(TestResolver)
         run_suites, run_tests = resolver.resolve_metadata(what)
 
         if not run_suites and not run_tests:
             print(UNKNOWN_TEST)
             return 1
 
         # Create shared logger
-        formatter = log_formatters[self._mach_context.settings['test']['format']][0]()
-        formatter.summary_on_shutdown = True
-
-        level = self._mach_context.settings['test']['level']
-        log = StructuredLogger('mach-test')
-        log.add_handler(StreamHandler(sys.stdout, LogLevelFilter(formatter, level)))
+        default_format = self._mach_context.settings['test']['format']
+        default_level = self._mach_context.settings['test']['level']
+        log = setup_logging('mach-test', log_args, {default_format: sys.stdout},
+                            {'level': default_level})
+        log.handlers[0].formatter.inner.summary_on_shutdown = True
 
         status = None
         for suite_name in run_suites:
             suite = TEST_SUITES[suite_name]
             kwargs = suite['kwargs']
             kwargs['log'] = log
 
             if 'mach_command' in suite:
--- a/testing/mochitest/mach_commands.py
+++ b/testing/mochitest/mach_commands.py
@@ -274,19 +274,17 @@ def verify_host_bin():
 @CommandProvider
 class MachCommands(MachCommandBase):
     @Command('mochitest', category='testing',
              conditions=[is_buildapp_in(*SUPPORTED_APPS)],
              description='Run any flavor of mochitest (integration test).',
              parser=setup_argument_parser)
     def run_mochitest_general(self, flavor=None, test_objects=None, resolve_tests=True, **kwargs):
         from mochitest_options import ALL_FLAVORS
-        from mozlog.commandline import log_formatters
-        from mozlog.handlers import StreamHandler, LogLevelFilter
-        from mozlog.structuredlog import StructuredLogger
+        from mozlog.commandline import setup_logging
 
         buildapp = None
         for app in SUPPORTED_APPS:
             if is_buildapp_in(app)(self):
                 buildapp = app
                 break
 
         flavors = None
@@ -297,22 +295,21 @@ class MachCommands(MachCommandBase):
                         continue
                     flavors = [fname]
                     break
         else:
             flavors = [f for f, v in ALL_FLAVORS.iteritems() if buildapp in v['enabled_apps']]
 
         if not kwargs.get('log'):
             # Create shared logger
-            formatter = log_formatters[self._mach_context.settings['test']['format']][0]()
-            formatter.summary_on_shutdown = True
-
-            level = self._mach_context.settings['test']['level']
-            kwargs['log'] = StructuredLogger('mach-mochitest')
-            kwargs['log'].add_handler(StreamHandler(sys.stdout, LogLevelFilter(formatter, level)))
+            default_format = self._mach_context.settings['test']['format']
+            default_level = self._mach_context.settings['test']['level']
+            kwargs['log'] = setup_logging('mach-mochitest', kwargs, {default_format: sys.stdout},
+                                          {'level': default_level})
+            kwargs['log'].handlers[0].formatter.inner.summary_on_shutdown = True
 
         from mozbuild.controller.building import BuildDriver
         self._ensure_state_subdir_exists('.')
 
         test_paths = kwargs['test_paths']
         kwargs['test_paths'] = []
 
         mochitest = self._spawn(MochitestRunner)