Logcat命令使用方法

Post on May 14, 2022 by Wei Lin

adb logcat命令

功能简介

​ 在终端使用adb logcat 命令,终端窗口会输出设备中日志缓存区的信息,可以根据相关参数控制输出格式和使用特定功能。

 # 查看帮助文档
 adb logcat -h/--help

-b 指定日志缓存区

Android日志系统中缓冲区包括:main,system,radio,events,crash,kernel

adb logcat				# 输出main,system,crash,kernel缓冲区日志
adb logcat -b main		# 输出main日志
adb logcat -b events		# 输出events日志
adb logcat -b main,system,radio,events,crash,kernel		# 输出所有缓冲区
adb logcat -b main,system,events		# 输出几个常用的缓冲区日志

注:输出多个缓冲区是,用”,”作为间隔,而不是空格(否则只输出第一个空格前的缓冲区)。

-c 清除日志缓存区

不指定缓冲区时默认清除main,system,crash,kernel缓存区域的日志(不包括event和radio)

指定缓冲区:

adb logcat -c -b events		# 清理events日志缓存
adb logcat -c -b radio
adb logcat -c -b main,system,radio,events,crash,kernel	# 清除所有

-v设置日志的输出格式

datetime		日志打印时间
pid				进程号
tid				线程号
priority		日志级别,如F、E、W、D、E
tag				日志标签
message			日志正文

adb logcat

说明:以默认方式打印日志,打印缓存区为main system radio events crash。

格式:

<datetime> <pid> <tid> <priority> <tag>: <message>

示例:

11-12 10:31:54.765  1555  1933 I ActivityManager: Start proc 27839:activity.mainprocess/u0a263 for pre-top-activity {com.example.activitylaunchdemo/com.example.activitylaunchdemo.MainActivity} caller=null

adb logcat -v brief

说明:打印简单日志信息

格式:

<priority>/<tag>(<pid>): <message>

如:

I/ActivityManager( 1555): Start proc 30779:activity.mainprocess/u0a263 for pre-top-activity {com.example.activitylaunchdemo/com.example.activitylaunchdemo.MainActivity} caller=null

adb logcat -v long

说明:打印全部信息,message单独列为一行

格式:

[ <datetime> <pid>: <tid> <priority>/<tag>]
<message>

如:

[ 11-12 11:03:20.884  1555: 1933 I/ActivityManager ]
Start proc 13512:activity.mainprocess/u0a263 for pre-top-activity {com.example.activitylaunchdemo/com.example.activitylaunchdemo.MainActivity} caller=null

adb logcat -v process

说明:除优先级和正文外,只输出日志的PID

格式:

<priority>(<pid>) <message>

如:

I( 1555) Start proc 32656:activity.mainprocess/u0a263 for pre-top-activity {com.example.activitylaunchdemo/com.example.activitylaunchdemo.MainActivity} caller=null  (ActivityManager)

adb logcat -v raw

说明:只输出正文

格式:

<message>

如:

Start proc 5737:activity.mainprocess/u0a263 for pre-top-activity {com.example.activitylaunchdemo/com.example.activitylaunchdemo.MainActivity} caller=null

adb logcat -v tag

说明:除优先级和正文外,只输出日志的标签

格式:

<priority>/<tag>: <message>

如:

I/ActivityManager: Start proc 3570:activity.mainprocess/u0a263 for pre-top-activity {com.example.activitylaunchdemo/com.example.activitylaunchdemo.MainActivity} caller=null

adb logcat -v thread

说明:输出优先级、pid、tid和正文

格式:

<priority>(<pid>: <tid>) <message>

如:

I( 1798: 1952) Start proc 21874:com.demoapp.activitydemo/u0a253 for pre-top-activity {com.demoapp.activitydemo/com.demoapp.activitydemo.MainActivity} caller=null

adb logcat -v threadtime

说明:与adb logcat打印出的格式一致

格式:

<datetime> <pid> <tid> <priority> <tag>: <message>

如:

11-12 11:08:49.037  1555  1933 I ActivityManager: Start proc 14934:activity.mainprocess/u0a263 for pre-top-activity {com.example.activitylaunchdemo/com.example.activitylaunchdemo.MainActivity} caller=null

adb logcat -v time

说明:需要打印日志详细时间的简单数据

格式:

<datetime> <priority>/<tag>(<pid>): <message>

如:

11-12 10:32:52.313 I/ActivityManager( 1555): Start proc 28972:activity.mainprocess/u0a263 for pre-top-activity {com.example.activitylaunchdemo/com.example.activitylaunchdemo.MainActivity} caller=null

使用tag或priority过滤日志

当未指定tag时(即*),默认输出D及以上级别。即adb logcat *等同于adb logcat *:D

当指定tag时但未指定priority时,默认输出全部级别。adb logcat tag等同于adb logcat tag:V

按tag和priority过滤日志

adb logcat <tag>:<priority> *:S
注:
···一定要有*:S,意思是按照过滤器的限制进行输出,否则标签或级别过滤都无效;
···标签要区分大小写,但<priority>与*:S不区分大小写;
···可以写多个<tag>:<priority>,之间用空格分隔。

如:

adb logcat ActivityManager:W *:S
打印标签为ActivityManager,级别W以上的日志。

adb logcat ActivityManager:W surfaceflinger:I *:S
打印标签ActivityManager:级别W以上以及标签surfaceflinger:级别I的日志。

按tag过滤日志

adb logcat <tag> *:S
注:
···<tag>等同于<tag>:V,表示某标签下所有级别的日志;
···可以写多个<tag>,之间用空格分隔。
如:
adb logcat ActivityManager *:S		# 打印标签为ActivityManager的日志

按priority过滤日志

adb logcat *:<priority>
注:
···显示优先级为<priority>及更高级别的日志信息;
···只按级别过滤时不需要*:S

如:
adb logcat *:E		# 打印级别为E以上的日志

不打印某tag

格式:adb logcat <tag>:S
说明:不打印标签为<tag>的日志
如:adb logcat ActivityManager:S	# 不打印标签为ActivityManager的日志

其它过滤方式

-s 等同于*:S

等同于*:S

-d 按pid过滤

命令:adb logcat -d --pid=<pid>
如:adb logcat -d --pid=22170		# 打印pid为22170的进程的日志

-e 正则表达式过滤

adb logcat -e regularExp

grep 管道过滤

# 输出包含指定字符串的行
adb logcat |grep Keyword 

输出到文件

adb logcat -f path

说明:输出到手机上的文件

# 将日志输出到手机的/sdcard/log.txt
adb logcat -f /sdcard/log.txt

adb logcat > path

说明:将日志输出到终端(电脑)路径

adb logcat > D:\log.txt