玩转 Appium 中 logger
log形式首先我们来看一段log输出:
info: Starting App
info: Attempting to kill all 'uiautomator' processes
info: Getting all processes with 'uiautomator'
info: executing cmd: /Users/wuxian/Documents/tools/sdk/platform-tools/adb -s emulator-5554 shell "ps 'uiautomator'"
info: No matching processes found
info: Running bootstrap
info: 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: INSTRUMENTATION_STATUS: numtests=1
info: INSTRUMENTATION_STATUS: stream=
info: io.appium.android.bootstrap.Bootstrap:
info: INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
info: INSTRUMENTATION_STATUS: test=testRunServer
info: INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
info: INSTRUMENTATION_STATUS: current=1
info: INSTRUMENTATION_STATUS_CODE: 1
info: Socket opened on port 4724
info: Appium Socket Server Ready我将上面的log分为4种(message为消息体)
log等级:message
log等级: message
log等级: message
log等级: message
1.log等级:message
这一类log就是简单的appium服务器的log,切log等级为非debug
2.log等级: message
这一类log和上面是一样的,都是appium服务器的log,区别在于该log等级为debug,在logger.js模
块中我们可以看到如下代码,下面的代码将debug等级的log,更改为info等级,然后在后面跟上
的标志。
if (levels === levels.debug) {
logger.debug = function (msg) { logger.info(' ' + msg); };
}
3. message
这一类log为手机中的socket服务器包(放在android手机端的jar包称为bootstrap)返回的输出
4.log等级: message
这一类log为执行case输出的log,我们可以理解为adb接受的log。我们一般执行uiautomator的
case时候,控制台输出的就是这类带有uiautomator标识的log。
自定义log部分
log等级
第一步我们来修改log等级。比如我们想将info级别改为warn级别,只需要将logger.js的223行左
右的如下代码
if (levels === levels.debug) {
logger.debug = function (msg) { logger.info(' ' + msg); };
}
修改为
这里就将debug等级的修改为了warn等级的(我之前说过,你应该知道为什么改变的是debug而不
是info等级吧?)。我们来看看输出:
info: Starting App
warn: Attempting to kill all 'uiautomator' processes
warn: Getting all processes with 'uiautomator'
warn: executing cmd: /Users/wuxian/Documents/tools/sdk/platform-tools/adb -s emulator-5554 shell "ps 'uiautomator'"
warn: No matching processes found
warn: Running bootstrap
warn: 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: INSTRUMENTATION_STATUS: numtests=1
warn: INSTRUMENTATION_STATUS: stream=
warn: io.appium.android.bootstrap.Bootstrap:
warn: INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
warn: INSTRUMENTATION_STATUS: test=testRunServer
warn: INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
warn: INSTRUMENTATION_STATUS: current=1
warn: INSTRUMENTATION_STATUS_CODE: 1
warn: Socket opened on port 4724
warn: Appium Socket Server Ready我们将info改变成了warn,但是还是存在info标志的,因为上面代码的改变是建立在你调用的logge
r.debug,因为我们之前说过了,appium会将debug等级改为info,然后在后面加一个标志,
现在我们改为warn,那么之前debug会改为warn,然后加一个标志。所以凡是warn后面必
然会跟一个。
debug标识
上面的debug,会在info/warn/error标识后面加一个,是不是很丑,我是觉得很丑,我们将
其改变一下改成,还是刚才的代码:
info: Starting App
warn: Attempting to kill all 'uiautomator' processes
warn: Getting all processes with 'uiautomator'
warn: executing cmd: /Users/wuxian/Documents/tools/sdk/platform-tools/adb -s emulator-5554 shell "ps 'uiautomator'"
warn: No matching processes found
warn: Running bootstrap
warn: 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: INSTRUMENTATION_STATUS: numtests=1
warn: INSTRUMENTATION_STATUS: stream=
warn: io.appium.android.bootstrap.Bootstrap:
warn: INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
warn: INSTRUMENTATION_STATUS: test=testRunServer
warn: INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
warn: INSTRUMENTATION_STATUS: current=1
warn: INSTRUMENTATION_STATUS_CODE: 1
warn: Socket opened on port 4724
warn: Appium Socket Server Ready
ok了。觉得UIAUTOMATOR STDOUT和BOOTSTRAP不理解?
没关系,写成中文,在devices/android/uiautomator.js文件中,找到190和203行左右的语句,将
上面两个标识符修改为中文:
修改前:
修改后:
输出:
info: Starting App
warn: Attempting to kill all 'uiautomator' processes
warn: Getting all processes with 'uiautomator'
warn: executing cmd: /Users/wuxian/Documents/tools/sdk/platform-tools/adb -s emulator-5554 shell "ps 'uiautomator'"
warn: No matching processes found
warn: Running bootstrap
warn: 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: [脚本输出] INSTRUMENTATION_STATUS: numtests=1
warn: [脚本输出] INSTRUMENTATION_STATUS: stream=
warn: [脚本输出] io.appium.android.bootstrap.Bootstrap:
warn: [脚本输出] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
warn: [脚本输出] INSTRUMENTATION_STATUS: test=testRunServer
warn: [脚本输出] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
warn: [脚本输出] INSTRUMENTATION_STATUS: current=1
warn: [脚本输出] INSTRUMENTATION_STATUS_CODE: 1
warn: [设备socket服务器输出] Socket opened on port 4724
warn: [设备socket服务器输出] Appium Socket Server Ready
页:
[1]