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
祝你好运