[autotest] Record log for abort_suite

Currently, it's impossible to debug whether a suite is aborted
by builder or by lab bug.
Record log in abort_suite module so that we can track back when
a suite is aborted via the module.

BUG=chromium:440905
TEST=./site_utils/abort_suite.py -s dummy -i nyan_big-release/R38-6029.0.0

Change-Id: Ief636ebe547b47db4b563b81edb9147acf1afea4
Reviewed-on: https://chromium-review.googlesource.com/234436
Reviewed-by: Fang Deng <fdeng@chromium.org>
Tested-by: Mungyung Ryu <mkryu@google.com>
Commit-Queue: Mungyung Ryu <mkryu@google.com>
diff --git a/server/site_utils.py b/server/site_utils.py
index 22479e9..c49030e 100644
--- a/server/site_utils.py
+++ b/server/site_utils.py
@@ -393,3 +393,29 @@
     }
     return ('%(prefix)s.%(board)s.%(build_type)s.%(branch)s.%(suite)s'
             % data_key_dict)
+
+
+def setup_logging(logfile=None):
+    """Setup basic logging with all logging info stripped.
+
+    Calls to logging will only show the message. No severity is logged.
+
+    @param logfile: If specified dump output to a file as well.
+    """
+    # Remove all existing handlers. client/common_lib/logging_config adds
+    # a StreamHandler to logger when modules are imported, e.g.,
+    # autotest_lib.client.bin.utils. A new StreamHandler will be added here to
+    # log only messages, not severity.
+    logging.getLogger().handlers = []
+
+    screen_handler = logging.StreamHandler()
+    screen_handler.setFormatter(logging.Formatter('%(asctime)s '
+            '%(levelname)-5s| %(message)s'))
+    logging.getLogger().addHandler(screen_handler)
+    logging.getLogger().setLevel(logging.INFO)
+    if logfile:
+        file_handler = logging.FileHandler(logfile)
+        file_handler.setFormatter(logging.Formatter('%(asctime)s '
+                '%(levelname)-5s| %(message)s'))
+        file_handler.setLevel(logging.DEBUG)
+        logging.getLogger().addHandler(file_handler)
diff --git a/site_utils/abort_suite.py b/site_utils/abort_suite.py
index 680be87..393f0f7 100755
--- a/site_utils/abort_suite.py
+++ b/site_utils/abort_suite.py
@@ -26,13 +26,17 @@
 import argparse
 import getpass
 import logging
+import os
 import sys
+from datetime import datetime
 
 import common
 from autotest_lib.client.common_lib.cros.graphite import stats
 from autotest_lib.server import frontend
+from autotest_lib.server import utils
 
 
+LOG_NAME_TEMPLATE = 'abort_suite-%s.log'
 SUITE_JOB_NAME_TEMPLATE = '%s-test_suites/control.%s'
 _timer = stats.Timer('abort_suites')
 
@@ -76,8 +80,17 @@
 
 def main():
     """Main."""
-    afe = frontend.AFE()
     args = parse_args()
+
+    log_dir = os.path.join(common.autotest_dir, 'logs')
+    if not os.path.exists(log_dir):
+        os.makedirs(log_dir)
+    log_name = LOG_NAME_TEMPLATE % args.build.replace('/', '_')
+    log_name = os.path.join(log_dir, log_name)
+
+    utils.setup_logging(logfile=log_name)
+
+    afe = frontend.AFE()
     name = SUITE_JOB_NAME_TEMPLATE % (args.build, args.name)
     abort_suites(afe, name)
     return 0
diff --git a/site_utils/run_suite.py b/site_utils/run_suite.py
index b5787e1..c77391e 100755
--- a/site_utils/run_suite.py
+++ b/site_utils/run_suite.py
@@ -44,7 +44,6 @@
 from datetime import datetime
 
 import common
-
 from autotest_lib.client.common_lib import error
 from autotest_lib.client.common_lib import global_config, enum
 from autotest_lib.client.common_lib import priorities
@@ -90,29 +89,6 @@
     return code1 if SEVERITY[code1] >= SEVERITY[code2] else code2
 
 
-def setup_logging(logfile=None):
-    """Setup basic logging with all logging info stripped.
-
-    Calls to logging will only show the message. No severity is logged.
-
-    @param logfile: If specified dump output to a file as well.
-    """
-    # Remove all existing handlers. client/common_lib/logging_config adds
-    # a StreamHandler to logger when modules are imported, e.g.,
-    # autotest_lib.client.bin.utils. A new StreamHandler will be added here to
-    # log only messages, not severity.
-    logging.getLogger().handlers = []
-
-    screen_handler = logging.StreamHandler()
-    screen_handler.setFormatter(logging.Formatter('%(message)s'))
-    logging.getLogger().addHandler(screen_handler)
-    logging.getLogger().setLevel(logging.INFO)
-    if logfile:
-        file_handler = logging.FileHandler(logfile)
-        file_handler.setLevel(logging.DEBUG)
-        logging.getLogger().addHandler(file_handler)
-
-
 def parse_options():
     #pylint: disable-msg=C0111
     usage = "usage: %prog [options]"
@@ -1245,7 +1221,7 @@
         if os.path.exists(log_dir):
             log_name = os.path.join(log_dir, log_name)
 
-    setup_logging(logfile=log_name)
+    utils.setup_logging(logfile=log_name)
 
     if not options.bypass_labstatus:
         utils.check_lab_status(options.build)