logcat

742 阅读3分钟

通过shell命令运行logcat

[adb] logcat [<option>] ... [<filter-spec>] ...

使用adb命令查看log,需要在 SDK platform-tools 目录下运行

adb logcat

查看logcat帮助信息

adb logcat --help

或者通过shell连接到指定设备,执行

$ adb shell
# logcat

Options

-b <buffer> 加载一个可使用的日志缓冲区供查看,默认值是main, 

-b main 是指定读取main缓冲区中的信息:
main 指的就是 /dev/log/main 这个循环buffer
ls 发现/dev/log/还有这几个循环buffer,一般读取main就够了
 system 输出系统组件的log
 radio  输出通信系统的log
 events  输出event模块的log
 main    所有java层的log,不属于上面3层的log

adb logcat –b radio

adb logcat –b system

adb logcat –b events

adb logcat –b main

-c, --clear 清除选中缓冲区的日志并退出,清除所有缓冲区 -b all -c.

-d  将缓冲区的log输出到屏幕中然后退出

-f <filename> 将log输出到指定的文件中<文件名>.默认为标准输出 stdout

-g, --buffer-size 打印指定日志缓冲区的大小并退出

-n <count> 设置日志的最大数目<count>,默认值是4,需要和-r选项一起使用

-r <kbytes><kbytes>时输出日志,默认值是16,需要和-f选项一起使用

-s  和 filterspec '*:S'  等效 ,设置过滤器 

-v <format> 设置日志输入格式,默认是 threadtime

一般长时间输出log的话建议-f,-n,-r三个参数连用,这样当一个文件日志输出满了之后可以马上在另一个中进行输出

//将缓冲区的log打印到屏幕并退出
adb logcat -d 
//清除缓冲区logtestCase运行前可以先清除一下)
adb logcat -c
//打印缓冲区大小并退出
adb logcat -g
//输出log
adb logcat -f /data/local/tmp/log.txt -n 10 -r 1

-t <count> 输出最近的 <count> 行日志,包含 -d 功能

--pid=<pid> ... 打印指定pid的日志

Filtering log output

  • V: Verbose (lowest priority)
  • D: Debug
  • I: Info
  • W: Warning
  • E: Error
  • F: Fatal
  • S: Silent (highest priority, on which nothing is ever printed)

过滤TAG为ActivityManager输出级别大于I的日志与TAG为MyApp输出级别大于D的日志

adb logcat ActivityManager:I MyApp:D *:S

Using *:S is an excellent way to ensure that log output is restricted to the filters that you have explicitly specified — it lets your filters serve as a "whitelist" for log output.

输入警告日志:

adb logcat *:W

通过环境变量android_log_tag设置默认筛选器

export ANDROID_LOG_TAGS="ActivityManager:I MyApp:D *:S"

Control log output format

  • brief: 显示优先级/标记和过程的PID发出的消息
  • long: 显示所有元数据字段与空白行和单独的消息
  • process: 只显示PID
  • raw: 显示原始的日志消息,没有其他元数据字段
  • tag: 只显示优先级/标记
  • thread: A legacy format that shows priority, PID, and TID of the thread issuing the message.
  • threadtime (default): 调用显示日期、时间、优先级、标签遗迹PID TID线程发出的消
  • time: 调用显示日期、时间、优先级/标签和过程的PID发出消息

[adb] logcat [-v <format>] 指定想要输出格式-v选项(只能指定一个输出格式)

adb logcat –v time

Viewing alternative log buffers

  • radio: View the buffer that contains radio/telephony related messages.
  • events: View the interpreted binary system event buffer messages.
  • main: View the main log buffer (default) does not contain system and crash log messages.
  • system: View the system log buffer (default).
  • crash: View the crash log buffer (default).
  • all: View all buffers.
  • default: Reports main, system, and crash buffers.

使用 -b 来显示指定缓冲区的日志

[adb] logcat [-b <buffer>]
adb logcat -b radio
logcat -b main -b radio -b events
logcat -v main,radio,events

官方文档