转载请注明出处:
http://blog.youkuaiyun.com/brucehurrican/article/details/70143310
现在 app 大部分在 debug 和 release 阶段都会在手机本地储存日志类文件,最近我在开发某个功能模块时,需要获取相应的日志信息,用 AS 的 logcat 已经达不到要求(因为日志太多了)并且 logcat 的日志不能在 AS上保存到本地。只能通过打印日志到手机 SD卡上的文件,导出后再分析。
一般的操作流程是 在获取信息的模块处打印日志,并将日志信息设置成保存到 SD卡上,编译安装,触发该功能模块,将日志取出,过滤信息。
如果测试设备数量少,这样手工操作没有什么问题。但是数量一多的话,就有点累人了。都说程序员比较懒,我也想偷会儿懒。写了个脚本,自动安装连接电脑 app,并在信息写入 sd 卡后,自动获取相应的日志信息并过滤关键字段。
为了方便说明,我在手机 sd 卡中 test/test 文件保存了如下日志信息
2017-04-10 14-22-02 test 当前设备型号 HM NOTE 1LTE
2017-04-10 14-22-08 test 请求url:http://www.csdn.com
2017-04-10 14-24-18 test hello world
2017-04-10 14-26-08 test hello java
2017-04-10 14-27-11 test hello android
2017-04-10 14-27-19 test result code: 200
2017-04-10 14-34-11 test hello android
2017-04-10 14-42-02 test 当前设备型号 HM NOTE 1LTE
2017-04-10 14-44-01 test 当前设备型号 HM NOTE 1LTE
2017-04-10 14-52-06 test 当前设备型号 HM NOTE 1LTE
2017-04-10 14-55-12 test 测试结果:ok
我要取出的信息是 当前设备型号,请求 url,测试结果 并去重。
脚本如下,脚本文件名为 getLogInfo.sh
#!/usr/bin/env zsh
echo '开始从 SD 卡中复制文件'
adb pull /mnt/sdcard/test/ /Users/tt/Documents/test/temp/
echo '复制文件结束'
echo '重命名文件并读取日志内容'
echo '清空临时文件内容'
echo '' > temp/temp.txt
for file in temp/test/*
do
echo `basename $file`
cp $file temp/$1`basename $file`.txt
echo '-----------获取设备信息------------'
grep -E '当前设备型号|请求url' temp/$1`basename $file`.txt >> temp/temp.txt
echo '------------test----------'
# grep -E '测试结果' temp/$1`basename $file`.txt | head -1 // 只取第一次出现结果
grep -E '测试结果' temp/$1`basename $file`.txt | awk '{print $4}' | sort | uniq >> temp/temp.txt // 去重
echo '-----------test-----------'
echo '===============我是分界线================='
done
echo '操作结束'
使用时可以给重命名的文件加前缀,通过 getLogInfo.sh kk-
这种方式即可。
以上就是我为了”偷懒”写的,此处记下,以免忘记了ʅ(´◔౪◔)ʃ