Appium 玩转 Appium 中 logger

易寒 · 2015年04月30日 · 最后由 enumerate 回复于 2016年02月24日 · 1727 次阅读
本帖已被设为精华帖!

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

哦了,就毁到这里吧。

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 18 条回复 时间 点赞

你的帖子,我必须第一个。真爱,另外不明觉厉!

#1 楼 @monkey 目送你被删

请问怎么能让 windows 的分颜色显示啊!也想搞个像 MAC 的可以快速区分的

#3 楼 @hzy780322 你说的编辑器是么?mac 下用的是 sublime,windows 版下一个就行。

好东西 还是要支持啊, 都是直接写个简单的 framework 直接用, 真没考虑过这些,也没仔细的看看 js 的文件

#1 楼 @monkey 到现在竟然还坚挺地没被删!?

@doctorq 有个地方我提议详细描述一下:

UIAUTOMATOR STDOUT是系统执行 uiautomator 时显示在 shell 中的信息(在操作系统层面都称为 STDOUT ,即标准输出 (对应地,标准输入为 STDIN,错误输出为 STDERR)。我们编程中使用 print 语句输出的对象就是 STDOUT ),BOOTSTRAP 是 bootstrap.jar 内部的 socket server 与 appium server 通讯的信息(通过 bootstrap port 输出),我觉得意义上这才是 bootstrap 内部操作的 log 。

单纯说 脚本输出 我觉得有些人可能会误解为是 bootstrap 让这些东西输出的,因为实质上是 UIAutomator 的执行过程自动输出的。

写 appium log 不说下 webhook 我觉得还不够。

#7 楼 @lihuazhang what is webhook

#8 楼 @doctorq appium --webhook localhost:3000

真好 直接动源码的都是大神!! 😄 :plus1:

安卓的有吗,新手 ios 的看不懂

易寒 #12 · 2015年05月11日 Author

#11 楼 @yan8718 这个是服务器的源码,android 和 ios 的都一样

mark 一下慢慢看 慢慢学

楼主你好,看了你的文章很赞,我想问一下,appium 运行时控制台输出的日志,可不可以重定向输出到 1 个 txt 文件里,我没有找到这类相关的方法,隔了快 1 年的时间,不知道你能不能看到我的回复,如果看到了,希望可以跟我说下,谢谢

易寒 #15 · 2016年02月24日 Author

#14 楼 @enumerate 修改源码可以做到,原生的 log 器应该没有保存到文件中

感谢楼主的回复,我现在的方法是在开启 appium 的时候写一个>>d:/log.txt 这样控制台的 log 就会都打在这个 txt 文件里了用这个方法就可以解决我的问题了,但里面有个问题就是,我只是想看到我的测试 case 的日志,其他的东西对我都没有用,可不可修改源码只输出测试 case 就是你说的
只输出这个

易寒 #17 · 2016年02月24日 Author

#16 楼 @enumerate 在只需要保存的 log 的地方才做保存到 txt 动作啊,完事以后在 txt 中过滤下也行啊

哦,好的,多谢了

需要 登录 後方可回應,如果你還沒有帳號按這裡 注册