logcat的使用
端上日志記錄可以使用工具: logger、LogUtils、timber等工具進行日志美化
(一)基礎用法與輸出
直接終端打印日志
adb logcat
終端打印日志并輸出
adb logcat > $(date "+%Y%m%d_%H:%M:%S").log
清除緩存信息
adb logcat -c
I/SDK ( 1306): [Server 5.2.0][2023-02-18 13:50:01.85563][DEBUG][access_cae_local.c:660][handle_local_request][ret:0]
I/SDK ( 1306): [CAE][D][2023-02-18 13:50:01.086766][cae_mqtt_client_publish_1:600][pub(1) msg 0xdac1ee20 120(mqtt id 102) Dev/7864E6E37F6C/Rpt/Attr: {"attrs":[{"humidificationStatus":"false"},{"co2Value":"0"},{"windDirectionVerticalL":"5"},{"selfCleaningStatus":"false"},{"windDirectionVerticalR":"5"},{"targetHumidity":"52"},{"indoorTemperature":"0"},{"freshAirStatus":"false"},{"healthMode":"false"},{"windDirectionHorizontalL":"0"},{"opSrc":"0"},{"windSpeedR":"5"},{"windSpeedL":"5"},{"screenDisplayStatus":"true"},{"onOffStatus":"false"},{"indoorPM2p5Value":"9"},{"muteStatus":"false"},{"electricHeatingStatus":"false"},{"operationMode":"0"},{"indoorHumidity":"39"},{"humanSensingStatus":"0"},{"errCode":"0"},{"rapidMode":"false"},{"silentSleepStatus":"false"},{"targetTemperature":"26"},{"windDirectionHorizontalR":"0"},{"ErrAckFlag":"false"},{"specialMode":"0"},{"tempUnit":"1"},{"pmvStatus":"false"},{"intelligenceStatus":"false"},{"halfDegreeSettingStatus":"false"},{"10degreeHeatingStatus":"false"},{"echoStatus":"false"}
(二)過濾關鍵字
"-s"選項 : 設置輸出日志的標簽, 只顯示該標簽的日志;
過濾標簽
adb logcat -s StateMachine
沒過濾的打印信息 I= INFO、 E=ERROR 、D=DEBUG
I/SDK ( 1306): [Server 5.2.0][2023-02-18 13:59:30.440423][DEBUG][access_cae_local.c:660][handle_local_request][ret:0]
E/StateMachine( 693): ConnectedState (when=-5ms what=131155 arg1=1!CMD_RSSI_POLL 1 0 "AC" 0c:b5:27:26:aa:48 rssi=-51 f=2412 sc=60 link=65 tx=5.9, 0.0, 0.0 rx=6.9 bcn=0 [on:0 tx:0 rx:0 period:3002] from screen [on:0 period:1662725131] gl hn u24 rssi=-46 ag=0 hr ticks 0,0,0 ls-=0 [56,56,60,60,65] brc=0 lrc=0
E/StateMachine( 693): L2ConnectedState (when=-6ms what=131155 arg1=1!CMD_RSSI_POLL 1 0 "AC" 0c:b5:27:26:aa:48 rssi=-51 f=2412 sc=60 link=65 tx=5.9, 0.0, 0.0 rx=6.9 bcn=0 [on:0 tx:0 rx:0 period:1] from screen [on:0 period:1662725132] gl hn u24 rssi=-46 ag=0 hr ticks 0,0,0 ls-=0 [56,56,60,60,65] brc=0 lrc=0
E/StateMachine( 693): get link layer stats 0
D/HW ( 693): enter -->_send_command cmd=IFNAME=wlan0 SIGNAL_POLL
D/wpa_supplicant( 1156): wlan0: Control interface command 'SIGNAL_POLL'
過濾StateMachine后的日志
E/StateMachine( 693): mIsFullScanOngoing: false, mSendScanResultsBroadcast: false
E/StateMachine( 693): ConnectedState (when=-3ms what=131155 arg1=1!CMD_RSSI_POLL 1 0 "AC" 0c:b5:27:26:aa:48 rssi=-51 f=2412 sc=60 link=65 tx=3.5, 0.0, 0.0 rx=3.5 bcn=0 [on:0 tx:0 rx:0 period:3001] from screen [on:0 period:1662878974] gl hn u24 rssi=-46 ag=0 hr ticks 0,0,0 ls-=0 [56,56,56,56,61] brc=0 lrc=0
E/StateMachine( 693): L2ConnectedState (when=-3ms what=131155 arg1=1!CMD_RSSI_POLL 1 0 "AC" 0c:b5:27:26:aa:48 rssi=-51 f=2412 sc=60 link=65 tx=3.5, 0.0, 0.0 rx=3.5 bcn=0 [on:0 tx:0 rx:0 period:1] from screen [on:0 period:1662878975] gl hn u24 rssi=-46 ag=0 hr ticks 0,0,0 ls-=0 [56,56,56,56,61] brc=0 lrc=0
(三)根據日志等級進行過濾
日志優先級(priority):
V : Verbose (明細);
D : Debug (調試);
I : Info (信息);
W : Warn (警告);
E : Error (錯誤);
F : Fatal (嚴重錯誤);
S : Silent(最高,啥也不輸出)
過濾所有的錯誤信息 *為shell的關鍵字,必須要通過\轉義
將 Error、Fatal 和 Silent 日志輸出。
adb logcat \*:E
這個好像不太好用
adb logcat StateMachine:E
(四)通過Grep 過濾
adb logcat | grep StateMachine
通過grep + 正則匹配
adb logcat | grep "^..Machine"
logcat中篩選多個關鍵字:
篩選AA,BB,CC關鍵字: ^(.(AA|BB|CC)).$
adb logcat | grep "^(.(AA|BB|CC)).$"
不想看到AA,BB,CC關鍵字:
adb logcat | grep "((.*)[(AA|BB|CC)]).*$"
(五)日志加上時間等信息
-v brief (默認)"優先級 / 標簽 (進程ID) : 日志信息"
-v tag " 優先級 / 標簽 : 日志信息"
-v time 命令, 追加日志的輸出時間;
-v thread命令 ,追加日志的輸出線程信息
-v threadtime 命令, 追加日志的輸出時間和線程信息;
- 還有更多,請自行搜索
adb logcat -v threadtime
02-18 14:06:46.062 865 865 E NetworkController: No data sim selected
02-18 14:06:46.062 865 865 D DefaultStatusBarPlmnPlugin: into updateCarrierLabel, slotId=0, isSimInserted=false, isHasSimService=false
02-18 14:06:46.062 865 865 D DefaultStatusBarPlmnPlugin: into updateCarrierLabel, slotId=1, isSimInserted=false, isHasSimService=false
02-18 14:06:46.079 196 196 I SurfaceFlinger: [Built-in Screen (type:0)] fps:9.948966,dur:1105.64,max:126.51,min:72.47
祝你好運