android——adb logcat常用命令总结

1、查看adb logcat帮助信息

C:\Users\000>adb logcat --help
Usage: logcat [options] [filterspecs]
options include:
  -s              Set default filter to silent.
                  Like specifying filterspec '*:s'
  -f <filename>   Log to file. Default to stdout
  -r [<kbytes>]   Rotate log every kbytes. (16 if unspecified). Requires -f
  -n <count>      Sets max number of rotated logs to <count>, default 4
  -v <format>     Sets the log print format, where <format> is one of:

                  brief process tag thread raw time threadtime long

  -c              clear (flush) the entire log and exit
  -d              dump the log and then exit (don't block)
  -t <count>      print only the most recent <count> lines (implies -d)
  -g              get the size of the log's ring buffer and exit
  -b <buffer>     Request alternate ring buffer, 'main', 'system', 'radio'
                  or 'events'. Multiple -b parameters are allowed and the
                  results are interleaved. The default is -b main -b system.
  -B              output the log in binary
filterspecs are a series of
  <tag>[:priority]

where <tag> is a log component tag (or * for all) and priority is:
  V    Verbose
  D    Debug
  I    Info
  W    Warn
  E    Error
  F    Fatal
  S    Silent (supress all output)

'*' means '*:d' and <tag> by itself means <tag>:v

If not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.
If no filterspec is found, filter defaults to '*:I'

If not specified with -v, format is set from ANDROID_PRINTF_LOG
or defaults to "brief"

2、解析选项

– “-s”选项 : 设置输出日志的标签, 只显示该标签的日志;

–”-f”选项 : 将日志输出到文件, 默认输出到标准输出流中;

–”-r”选项 : 按照每千字节输出日志, 需要 -f 参数;

–”-n”选项 : 设置日志输出的最大数目, 需要 -r 参数;

–”-v”选项 : 设置日志的输出格式, 注意只能设置一项;

–”-c”选项 : 清空所有的日志缓存信息;

–”-d”选项 : 将缓存的日志输出到屏幕上, 并且不会阻塞;

–”-t”选项 : 输出最近的几行日志, 输出完退出, 不阻塞;

–”-g”选项 : 查看日志缓冲区信息;

–”-b”选项 : 加载一个日志缓冲区, 默认是 main, 下面详解;

–”-B”选项 : 以二进制形式输出日志;

3、输出指定标签的日志

方式1:设置默认的过滤器,-s选项+标签名
adb logcat -s TAG标签名 TAG标签名
如:

adb logcat -s VideoFragment VLCVideoPlayerActivity

方式2:自定义过滤器,指定日志级别
LEVEL可以选择:

– V : Verbose (明细);
– D : Debug (调试);
– I : Info (信息);
– W : Warn (警告);
– E : Error (错误);
– F: Fatal (严重错误);
– S : Silent(Super all output) (最高的优先级, 可能不会记载东西);

TAG:X 的作用为: 输出标签为TAG的log级别大于X的信息,例如:

adb logcat VideoFragment:D

输出 VideoFragment的D 和D级别以上的log,包括 D, I, W, E,F, S
注意:
(1)可以指定多个[TAG:LEVEL ]
(2) level : S 表示为不输出该标签的日志,应为没有大于S级别的日志了
(3) [TAG:LEVEL ] 不会影响其他标签的日志, 所以如果要屏蔽其他log请使用 *:S,例如:

adb logcat VideoFragment:D VLCVideoPlayerActivity:D *:S

方式3:使用管道过滤日志
linux系统下:

adb logcat | grep -e "VideoFragment\|VLCVideoPlayerActivity"
adb logcat | grep -e "VideoFragment|VLCVideoPlayerActivity"
adb logcat |grep -e VideoFragment -e VLCVideoPlayerActivity

windows系统下:
多个条件直接用空格

adb logcat | findstr "VideoFragment VLCVideoPlayerActivity"

4、输出日志信息到文件

方式1:-f选项

adb logcat -f /sdcard/log.txt 

方式2:输出重定向

adb logcat > /sdcard/log.txt  

后台执行

logcat -f /sdcard/log.txt &   #这里的&符号表示后台执行,不能少

注意合适的时候需要停止掉以上命令,必须有多个logcat在写同一个文件。
停止方法:

adb shell kill -9 <logcat_pid> 

其中logcat_pid 通过 如下命令获取

adb shell ps | grep logcat          # linux 平台
adb shell ps | findstr "logcat"    #Windows平台

5、-v 日志常用输出格式

1、“tag”格式 : ” 优先级 / 标签 : 日志信息”,例如:

C:\Users\000>adb logcat -v tag -s VideoFragment
D/VideoFragment: ====== onCreate
D/VideoFragment: =======bindVideoPlayerService
D/VideoFragment: UpdateAdapterTask doInBackground
D/VideoFragment: ====== onStart
D/VideoFragment: UpdateAdapterTask onPostExecute
D/VideoFragment: =====UpdateAdapterTask notifyDataSetChanged
D/VideoFragment: =====  PlayerService onServiceConnected

2、“time”格式 : “日期 时间 优先级 / 标签 (进程ID) : 进程名称 : 日志信息 “, 例如:

C:\Users\000>adb logcat -v time -s VideoFragment
04-25 15:04:39.591 D/VideoFragment( 5859): ====== onCreate
04-25 15:04:39.601 D/VideoFragment( 5859): =======bindVideoPlayerService
04-25 15:04:39.621 D/VideoFragment( 5859): UpdateAdapterTask doInBackground
04-25 15:04:39.621 D/VideoFragment( 5859): ====== onStart
04-25 15:04:39.641 D/VideoFragment( 5859): UpdateAdapterTask onPostExecute
04-25 15:04:39.641 D/VideoFragment( 5859): =====UpdateAdapterTask notifyDataSetChanged
04-25 15:04:39.781 D/VideoFragment( 5859): =====PlayerService onServiceConnected

6、日志缓冲区

C:\Users\000>adb shell
root@rk3288:/ # ll /dev/log
crw-rw-rw- root     log       10,  46 2016-04-25 10:50 events #事件相关的日志信息
crw-rw-rw- root     log       10,  47 2016-04-25 10:50 main #默认的缓冲区
crw-rw-rw- root     log       10,  45 2016-04-25 10:50 radio #广播电话相关的日志信息
crw-rw-rw- root     log       10,  44 2016-04-25 10:50 system #与系统相关的日志信息

查看日志缓冲区信息:

C:\Users\000>adb logcat -g
/dev/log/main: ring buffer is 256Kb (40Kb consumed), max entry is 5120b, max pay
load is 4076b
/dev/log/system: ring buffer is 256Kb (11Kb consumed), max entry is 5120b, max p
ayload is 4076b

清空日志缓冲区:

C:\Users\000>adb logcat -c

C:\Users\000>adb logcat -g
/dev/log/main: ring buffer is 256Kb (0Kb consumed), max entry is 5120b, max payl
oad is 4076b
/dev/log/system: ring buffer is 256Kb (0Kb consumed), max entry is 5120b, max pa
yload is 4076b
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值