appium 中的 log 输出量很大,我们分析问题的时候会依赖于这些 log,但是你理解这些 log 输出一些标志么?你是否觉得它的输出晦涩难懂了?想不想改成自己的大名?那就看下面的文章吧。

log 形式

首先我们来看一段 log 输出:

info: Starting App
info: [debug] Attempting to kill all 'uiautomator' processes
info: [debug] Getting all processes with 'uiautomator'
info: [debug] executing cmd: /Users/wuxian/Documents/tools/sdk/platform-tools/adb -s emulator-5554 shell "ps 'uiautomator'"
info: [debug] No matching processes found
info: [debug] Running bootstrap
info: [debug] spawning: /Users/wuxian/Documents/tools/sdk/platform-tools/adb -s emulator-5554 shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap -e pkg com.example.android.apis -e disableAndroidWatchers false
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
info: [debug] [UIAUTOMATOR STDOUT] io.appium.android.bootstrap.Bootstrap:
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 1
info: [debug] [BOOTSTRAP] [debug] Socket opened on port 4724
info: [debug] [BOOTSTRAP] [debug] Appium Socket Server Ready


我将上面的 log 分为 4 种(message 为消息体)

  1. log 等级:message
  2. log 等级:[debug] message
  3. log 等级:[debug] [BOOTSTRAP] [debug] message
  4. log 等级:[debug] [UIAUTOMATOR STDOUT] message

1.log 等级:message

这一类 log 就是简单的 appium 服务器的 log,切 log 等级为非 debug

2.log 等级:[debug] message

这一类 log 和上面是一样的,都是 appium 服务器的 log,区别在于该 log 等级为 debug,在 logger.js 模块中我们可以看到如下代码,下面的代码将 debug 等级的 log,更改为 info 等级,然后在后面跟上 [debug] 的标志。

if (levels[logger.transports.console.level] === levels.debug) {
    logger.debug = function (msg) { logger.info('[debug] ' + msg); };
  }

3.[debug] [BOOTSTRAP] [debug] message

这一类 log 为手机中的 socket 服务器包 (放在 android 手机端的 jar 包称为 bootstrap) 返回的输出

4.log 等级:[debug] [UIAUTOMATOR STDOUT] message

这一类 log 为执行 case 输出的 log,我们可以理解为 adb 接受的 log。我们一般执行 uiautomator 的 case 时候,控制台输出的就是这类带有 uiautomator 标识的 log。

自定义 log 部分

log 等级

第一步我们来修改 log 等级。比如我们想将 info 级别改为 warn 级别,只需要将 logger.js 的 223 行左右的如下代码

if (levels[logger.transports.console.level] === levels.debug) {
    logger.debug = function (msg) { logger.info('[debug] ' + msg); };
  }

修改为
这里写图片描述

这里就将 debug 等级的修改为了 warn 等级的 (我之前说过,你应该知道为什么改变的是 debug 而不是 info 等级吧?)。我们来看看输出:

info: Starting App
warn: [debug] Attempting to kill all 'uiautomator' processes
warn: [debug] Getting all processes with 'uiautomator'
warn: [debug] executing cmd: /Users/wuxian/Documents/tools/sdk/platform-tools/adb -s emulator-5554 shell "ps 'uiautomator'"
warn: [debug] No matching processes found
warn: [debug] Running bootstrap
warn: [debug] spawning: /Users/wuxian/Documents/tools/sdk/platform-tools/adb -s emulator-5554 shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap -e pkg com.example.android.apis -e disableAndroidWatchers false
warn: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
warn: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
warn: [debug] [UIAUTOMATOR STDOUT] io.appium.android.bootstrap.Bootstrap:
warn: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
warn: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
warn: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
warn: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
warn: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 1
warn: [debug] [BOOTSTRAP] [debug] Socket opened on port 4724
warn: [debug] [BOOTSTRAP] [debug] Appium Socket Server Ready

我们将 info 改变成了 warn,但是还是存在 info 标志的,因为上面代码的改变是建立在你调用的 logger.debug,因为我们之前说过了,appium 会将 debug 等级改为 info,然后在后面加一个 [debug] 标志,现在我们改为 warn,那么之前 debug 会改为 warn,然后加一个 [debug] 标志。所以凡是 warn 后面必然会跟一个 [debug]。

debug 标识

上面的 debug,会在 info/warn/error 标识后面加一个 [debug],是不是很丑,我是觉得很丑,我们将其改变一下改成 [TesterHome],还是刚才的代码:

这里写图片描述

info: Starting App
warn: [TesterHome] Attempting to kill all 'uiautomator' processes
warn: [TesterHome] Getting all processes with 'uiautomator'
warn: [TesterHome] executing cmd: /Users/wuxian/Documents/tools/sdk/platform-tools/adb -s emulator-5554 shell "ps 'uiautomator'"
warn: [TesterHome] No matching processes found
warn: [TesterHome] Running bootstrap
warn: [TesterHome] spawning: /Users/wuxian/Documents/tools/sdk/platform-tools/adb -s emulator-5554 shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap -e pkg com.example.android.apis -e disableAndroidWatchers false
warn: [TesterHome] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
warn: [TesterHome] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
warn: [TesterHome] [UIAUTOMATOR STDOUT] io.appium.android.bootstrap.Bootstrap:
warn: [TesterHome] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
warn: [TesterHome] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
warn: [TesterHome] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
warn: [TesterHome] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
warn: [TesterHome] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 1
warn: [TesterHome] [BOOTSTRAP] [debug] Socket opened on port 4724
warn: [TesterHome] [BOOTSTRAP] [debug] Appium Socket Server Ready

ok 了。

觉得 UIAUTOMATOR STDOUT 和 BOOTSTRAP 不理解?

没关系,写成中文,在 devices/android/uiautomator.js 文件中,找到 190 和 203 行左右的语句,将上面两个标识符修改为中文:
修改前:
这里写图片描述
修改后:
这里写图片描述

输出:

info: Starting App
warn: [TesterHome] Attempting to kill all 'uiautomator' processes
warn: [TesterHome] Getting all processes with 'uiautomator'
warn: [TesterHome] executing cmd: /Users/wuxian/Documents/tools/sdk/platform-tools/adb -s emulator-5554 shell "ps 'uiautomator'"
warn: [TesterHome] No matching processes found
warn: [TesterHome] Running bootstrap
warn: [TesterHome] spawning: /Users/wuxian/Documents/tools/sdk/platform-tools/adb -s emulator-5554 shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap -e pkg com.example.android.apis -e disableAndroidWatchers false
warn: [TesterHome] [脚本输出] INSTRUMENTATION_STATUS: numtests=1
warn: [TesterHome] [脚本输出] INSTRUMENTATION_STATUS: stream=
warn: [TesterHome] [脚本输出] io.appium.android.bootstrap.Bootstrap:
warn: [TesterHome] [脚本输出] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
warn: [TesterHome] [脚本输出] INSTRUMENTATION_STATUS: test=testRunServer
warn: [TesterHome] [脚本输出] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
warn: [TesterHome] [脚本输出] INSTRUMENTATION_STATUS: current=1
warn: [TesterHome] [脚本输出] INSTRUMENTATION_STATUS_CODE: 1
warn: [TesterHome] [设备socket服务器输出] [debug] Socket opened on port 4724
warn: [TesterHome] [设备socket服务器输出] [debug] Appium Socket Server Ready

哦了,就毁到这里吧。


↙↙↙阅读原文可查看相关链接,并与作者交流