1. adb logcat简单使用
# 1. 打印默认日志
adb logcat
# 2. 显示日志详细时间的简单数据
adb logcat -v time
# 3. 打印级别是Error的信息
adb logcat *.E
Usage: logcat [options] [filterspecs]
filterspecs 格式: <tag>[:priority]
where <tag> is a log component(组件) tag (or * for all) and priority is:
V Verbose (default for <tag>),默认级别,最低输出最多
D Debug (default for '*')
I Info
W Warn
E Error
F Fatal致命
S Silent (suppress all output)静音(抑制所有的输出)
'*' by itself means '*:D' and <tag> by itself means <tag>:V.
If no '*' filterspec or -s on command line, all filter defaults to '*:V'.
eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.
If not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.
<tag> 表示标签,用*表示所有,priority输出的级别
# 4. 将日志保存在PC指定位置,如c:\testing\log.txt
adb logcat -v time *:E >c:\testing\log.txt
没有过滤条件,log较多,很难找到想要的信息
2. 按级别过滤log
adb logcat <tag>[:priority]
3. 按tag和级别过滤日志
可以由多个[:priority]
组成
adb logcat PushService:I ActivityManager:W *:S
表示输出PushService的Info以上级别日志,ActivityManager的Warning以上级别日志,及其他tag的Slient级别日志(即屏蔽其他log)
4. adb logcat 选项解析
Usage: logcat [options] [filterspecs]
options include:
-s Set default filter to silent. Equivalent to filterspec '*:S'
相当于*:S,故无log输出
-f <file>, --file=<file> Log to file. Default is stdout标准输出流,-f输出不成功报错:couldn't open output file
-r <kbytes>, --rotate-kbytes=<kbytes>
Rotate log every kbytes. Requires -f option
按照千字节输出日志;与-f一起使用,但执行不成功
-n <count>, --rotate-count=<count>
Sets max number of rotated logs to <count>, default 4
设置log输出的最大数;执行效果感觉与adb logcat一样
-v <format>, --format=<format> 设置输出log的格式;已详说
-c, --clear Clear (flush) the entire log and exit
if Log to File specified, clear fileset instead
清空所有的log缓存信息
-d Dump the log and then exit (don't block)
将缓存日志输出到屏幕上,并且不会阻塞
-t <count> Print only the most recent <count> lines (implies -d)
输出最近几行log,输出后退出不阻塞
-t '<time>' Print most recent lines since specified time (implies -d)
-g, --buffer-size Get the size of the ring buffer.
查看log缓存区信息
-B, --binary Output the log in binary.
以二进制形式输出日志
# adb logcat -t <count>
C:\Windows\system32>adb logcat -t 5
--------- beginning of main
04-27 18:19:02.621 28185 28185 D UploadVoiceJobService: needUploadVoice timeSinceLastUpload = 2719490
04-27 18:19:03.518 2103 2145 D CompatibilityInfo: mCompatibilityFlags - 0
04-27 18:19:03.518 2103 2145 D CompatibilityInfo: applicationDensity - 440
04-27 18:19:03.518 2103 2145 D CompatibilityInfo: applicationScale - 1.0
04-27 18:19:06.578 28185 28204 I AnrWatcher: check anr normal9
C:\Windows\system32>
# ------------------------------------------------------------------------------------------------------
# adb logcat -g
C:\Windows\system32>adb logcat -g
main: ring buffer is 2Mb (473Kb consumed), max entry is 5120b, max payload is 4068b
system: ring buffer is 4Mb (34Kb consumed), max entry is 5120b, max payload is 4068b
crash: ring buffer is 1Mb (0b consumed), max entry is 5120b, max payload is 4068b
C:\Windows\system32>
4.1 adb logcat -v <format>
-v <format>, --format=<format> options:
Sets log print format verb and adverbs(副词), where <format> is:
brief long process raw tag thread threadtime time
and individually(单独) flagged modifying adverbs can be added:
color descriptive epoch monotonic printable uid usec UTC year zone
Single format verbs:
long — Display(显示) all metadata(元数据) fields, separate messages with blank lines.
raw — Display the raw(原始) log message, with no other metadata fields.
1) brief
默认输出格式:<priority>/<tag>(<pid>):<message>
C:\testing>adb logcat -t 3 -v brief
--------- beginning of main
I/encent.mobileq(26197): The ClassLoaderContext is a special shared library.
D/PatchLogTag(26197): DexPatchInstaller injectDexPatch result=500, inject cost time=9 ms.
W/System (26177): A resource failed to call close.
C:\testing>
2) long
输出格式:[<datetime> <pid>:<tid> <priority>/<tag>] <换行><message> <空行>
C:\testing>adb logcat -t 3 -v long
--------- beginning of main
[ 04-27 17:49:22.878 2632:11232 I/chatty ]
uid=1001(radio) Binder:2632_E expire 2 lines
[ 04-27 17:49:22.888 2632:11232 E/PhoneInterfaceManager ]
[PhoneIntfMgr] checkCarrierPrivilegesForPackage: No UICC
[ 04-27 17:49:26.119 26585:26604 D/NetworkStateReceiver ]
sWifiStrengthLevel = 3
C:\testing>
3) process
输出格式:<priority>(<pid>) <message>
C:\testing>adb logcat -t 3 -v process
--------- beginning of main
D( 2103) applicationScale - 1.0 (CompatibilityInfo)
I( 3338) GraphicDumpService onStartJob (MiuiPerfServiceClient)
I( 3338) GraphicDumpService onStopJob (MiuiPerfServiceClient)
C:\testing>
4) raw
输出格式:<message>
C:\testing>adb logcat -t 3 -v raw
--------- beginning of main
[PhoneIntfMgr] checkCarrierPrivilegesForPackage: No UICC
[PhoneIntfMgr] checkCarrierPrivilegesForPackage: No UICC
[process] filterRealForegroundProcMap: invoked. realProcMap: {com.tencent.mobileqq=1619516235572}
C:\testing>
5) tag
输出格式:<priority>/<tag>:<message>
C:\testing>adb logcat -t 3 -v tag
--------- beginning of main
D/UploadVoiceJobService: needUploadVoice timeSinceLastUpload = 554317
--------- beginning of system
E/storaged: getDiskStats failed with result NOT_SUPPORTED and size 0
D/KeyguardUpdateMonitor: handleTimeUpdate
C:\testing>
6) thread
输出格式:<priority>(<pid>:<uid>) <message>
C:\testing>adb logcat -t 3 -v thread
--------- beginning of main
D( 2412: 2412) handleTimeUpdate
D(26197:26262) 848QQDT [supplementReportForSwitchAccount] startTimestamp is 0
D(26302:26435) Kill MSF check result[ senderSize:0,sendInterval:949668,receiveInterval=55922,addCmdCount=9
C:\testing>
7) threadtime
输出格式:<datetime> <pid> <tid> <priority> <tag>:<message>
C:\testing>adb logcat -t 3 -v threadtime
--------- beginning of main
04-27 17:47:19.174 2412 2412 D KeyguardUpdateMonitor: handleBatteryUpdate
04-27 17:47:19.178 26585 26585 D UploadVoiceJobService: needUploadVoice timeSinceLastUpload = 816047
04-27 17:47:28.291 26197 26247 D GuardManager.GuardStateScheduler: [process] filterRealForegroundProcMap: invoked. realProcMap: {com.tencent.mobileqq=1619516235572}
C:\testing>
8) time
输出格式:<datetime> <priority>/<tag>(<pid>):<message>
C:\testing>adb logcat -t 3 -v time
--------- beginning of main
04-27 17:46:46.782 D/CompatibilityInfo( 2103): applicationDensity - 440
04-27 17:46:46.783 D/CompatibilityInfo( 2103): applicationScale - 1.0
04-27 17:46:52.261 D/GuardManager.GuardStateScheduler(26197): [process] filterRealForegroundProcMap: invoked. realProcMap: {com.tencent.mobileqq=1619516235572}
C:\testing>
-v time
:打印普通日志
-v threadtime
:查看线程
其他的使用较少
4.2 adb logcat -c
adb logcat -c
用来清除缓存信息
直接显示log,是会显示很多的,好像是从设备最近一次开机开始的
建议:先-c
清除,然后再触发事件,查看最近的日志信息
5. 日志脚本
5.1 clearLogCache.bat
adb logcat -c
5.2 getLog.bat
adb wait-for-device
adb devices
adb logcat -v threadtime >logs\"%date:~,4%%date:~5,2%%date:~8,2%_%time:~,2%%time:~3,2%%time:~6,2%_log.txt"
pause
5.3 获取指定包的log
@echo off
echo Please enter your PackageName:
SET /p PackageName =
echo Your PackageName is %PackageName %
adb logcat | findstr %PackageName % > logs\"%PackageName %_%date:~,4%%date:~5,2%%date:~8,2%_%time:~,2%%time:~3,2%%time:~6,2%_log.txt"
pause
养成adb脚本的编写,对重复性的adb命令操作有很大作用。