Repository: google/mobly

Issue:
Support custom logger prefix in AndroidDevice controller
Right now the logger prefix for Android device controller can not be changed.
Sometimes it is useful to be able to change the prefix to be more fitting for the context of the test.
E.g. Instead of `[AndroidDevice|<serial>]`, `[AndroidDevice|caller]` could be more useful in a call test.


Discussion:
Are you just thinking of addressing this using the testbed config? or with a method call after Android device controller initialization?
In AndroidDevice, not config, so it's explicit?

Resolving patch:
diff --git a/mobly/controllers/android_device.py b/mobly/controllers/android_device.py
--- a/mobly/controllers/android_device.py
+++ b/mobly/controllers/android_device.py
@@ -339,8 +339,10 @@ class AndroidDevice(object):
         serial: A string that's the serial number of the Androi device.
         log_path: A string that is the path where all logs collected on this
                   android device should be stored.
-        log: A logger adapted from root logger with added token specific to an
-             AndroidDevice instance.
+        log: A logger adapted from root logger with an added prefix specific
+             to an AndroidDevice instance. The default prefix is
+             [AndroidDevice|<serial>]. Use self.set_logger_prefix_tag to use
+             a different tag in the prefix.
         adb_logcat_file_path: A string that's the full path to the adb logcat
                               file collected, if any.
         adb: An AdbProxy object used for interacting with the device via adb.
@@ -354,7 +356,7 @@ def __init__(self, serial=""):
         log_path_base = getattr(logging, "log_path", "/tmp/logs")
         self.log_path = os.path.join(log_path_base, "AndroidDevice%s" % serial)
         self.log = AndroidDeviceLoggerAdapter(logging.getLogger(), {
-            "serial": self.serial
+            "tag": self.serial
         })
         self.sl4a = None
         self.ed = None
@@ -366,6 +368,24 @@ def __init__(self, serial=""):
             self.root_adb()
         self._snippet_clients = []
 
+    def set_logger_prefix_tag(self, tag):
+        """Set a tag for the log line prefix of this instance.
+
+        By default, the tag is the serial of the device, but sometimes having
+        the serial number in the log line doesn't help much with debugging. It
+        could be more helpful if users can mark the role of the device instead.
+
+        For example, instead of marking the serial number:
+            "INFO [AndroidDevice|abcdefg12345] One pending call ringing."
+
+        marking the role of the device here is  more useful here:
+            "INFO [AndroidDevice|Caller] One pending call ringing."
+
+        Args:
+            tag: A string that is the tag to use.
+        """
+        self.log.extra["tag"] = tag
+
     # TODO(angli): This function shall be refactored to accommodate all services
     # and not have hard coded switch for SL4A when b/29157104 is done.
     def start_services(self, skip_sl4a=False):
@@ -769,6 +789,16 @@ def reboot(self):
 
 
 class AndroidDeviceLoggerAdapter(logging.LoggerAdapter):
+    """A wrapper class that adds a prefix to each log line.
+
+    Usage:
+        my_log = AndroidDeviceLoggerAdapter(logging.getLogger(), {
+            "tag": <custom tag>
+        })
+
+        Then each log line added by my_log will have a prefix
+        "[AndroidDevice|<tag>]"
+    """
     def process(self, msg, kwargs):
-        msg = "[AndroidDevice|%s] %s" % (self.extra["serial"], msg)
+        msg = "[AndroidDevice|%s] %s" % (self.extra["tag"], msg)
         return (msg, kwargs)


Test patch: