1. 程式人生 > >快速從手機中取出除錯日誌並過濾關鍵欄位

快速從手機中取出除錯日誌並過濾關鍵欄位

現在 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-
這種方式即可。

以上就是我為了”偷懶”寫的,此處記下,以免忘記了ʅ(´◔౪◔)ʃ