Android的logcat日志详解

Android log系统

logcat介绍

logcatandroid中的一个命令行工具,可以用于得到程序的log信息。下面介绍 adb logcat中的详细参数命令以及如何才能高效的打印日志,或把日志保存到我们指定的位置。

可以输入 adb logcat --help,查看一下一些简单的数据格式:

adb logcat --help

输出结果:

Usage: logcat [options] [filterspecs]

General options:
  -b, --buffer=<buffer>       Request alternate ring buffer(s):
                                main system radio events crash default all
                              Additionally, 'kernel' for userdebug and eng builds, and
                              'security' for Device Owner installations.
                              Multiple -b parameters or comma separated list of buffers are
                              allowed. Buffers are interleaved.
                              Default -b main,system,crash,kernel.
  -L, --last                  Dump logs from prior to last reboot from pstore.
  -c, --clear                 Clear (flush) the entire log and exit.
                              if -f is specified, clear the specified file and its related rotated
                              log files instead.
                              if -L is specified, clear pstore log instead.
  -d                          Dump the log and then exit (don't block).
  --pid=<pid>                 Only print logs from the given pid.
  --wrap                      Sleep for 2 hours or when buffer about to wrap whichever
                              comes first. Improves efficiency of polling by providing
                              an about-to-wrap wakeup.

Formatting:
  -v, --format=<format>       Sets log print format verb and adverbs, where <format> is one of:
                                brief help long process raw tag thread threadtime time
                              Modifying adverbs can be added:
                                color descriptive epoch monotonic printable uid usec UTC year zone
                              Multiple -v parameters or comma separated list of format and format
                              modifiers are allowed.
  -D, --dividers              Print dividers between each log buffer.
  -B, --binary                Output the log in binary.

Outfile files:
  -f, --file=<file>           Log to file instead of stdout.
  -r, --rotate-kbytes=<n>     Rotate log every <n> kbytes. Requires -f option.
  -n, --rotate-count=<count>  Sets max number of rotated logs to <count>, default 4.
  --id=<id>                   If the signature <id> for logging to file changes, then clear the
                              associated files and continue.

Logd control:
 These options send a control message to the logd daemon on device, print its return message if
 applicable, then exit. They are incompatible with -L, as these attributes do not apply to pstore.
  -g, --buffer-size           Get the size of the ring buffers within logd.
  -G, --buffer-size=<size>    Set size of a ring buffer in logd. May suffix with K or M.
                              This can individually control each buffer's size with -b.
  -S, --statistics            Output statistics.
                              --pid can be used to provide pid specific stats.
  -p, --prune                 Print prune rules. Each rule is specified as UID, UID/PID or /PID. A
                              '~' prefix indicates that elements matching the rule should be pruned
                              with higher priority otherwise they're pruned with lower priority. All
                              other pruning activity is oldest first. Special case ~! represents an
                              automatic pruning for the noisiest UID as determined by the current
                              statistics.  Special case ~1000/! represents pruning of the worst PID
                              within AID_SYSTEM when AID_SYSTEM is the noisiest UID.
  -P, --prune='<list> ...'    Set prune rules, using same format as listed above. Must be quoted.

Filtering:
  -s                          Set default filter to silent. Equivalent to filterspec '*:S'
  -e, --regex=<expr>          Only print lines where the log message matches <expr> where <expr> is
                              an ECMAScript regular expression.
  -m, --max-count=<count>     Quit after printing <count> lines. This is meant to be paired with
                              --regex, but will work on its own.
  --print                     This option is only applicable when --regex is set and only useful if
                              --max-count is also provided.
                              With --print, logcat will print all messages even if they do not
                              match the regex. Logcat will quit after printing the max-count number
                              of lines that match the regex.
  -t <count>                  Print only the most recent <count> lines (implies -d).
  -t '<time>'                 Print the lines since specified time (implies -d).
  -T <count>                  Print only the most recent <count> lines (does not imply -d).
  -T '<time>'                 Print the lines since specified time (not imply -d).
                              count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'
                              'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format.
  --uid=<uids>                Only display log messages from UIDs present in the comma separate list
                              <uids>. No name look-up is performed, so UIDs must be provided as
                              numeric values. This option is only useful for the 'root', 'log', and
                              'system' users since only those users can view logs from other users.

filterspecs are a series of
  <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.

If not specified with -v on command line, format is set from ANDROID_PRINTF_LOG
or defaults to "threadtime"

log类是一个日志类,可以在代码中使用logcat打印出消息。常见的日志记录方法包括。

方法

描述

v(String,String) (vervbose)

显示全部信息

d(String,String)(debug)

显示调试信息

i(String,String)(information)

显示⼀般信息

w(String,String)(waning)

显示警告信息

e(String,String)(error)

显示错误信息

例如:

// 开发过程中获取 log
Log.i("MyActivity","MyClass.getView() - get item number"+position);
//adb 获取 log
adb logcat
adb logcat 输出的⽇志格式如下:
I/ActivityManager( 1754): Waited long enough for: ServiceRecord{2b24178c u0
com.google.android.gms/.checkin.CheckinService}

logcat命令格式

语法格式:

[adb] logcat [] … [ ] …
PC 端使⽤: adb logcat
shell 模式下使⽤: logcat

logcat缓冲区

缓冲区介绍

Android 日志系统为日志消息保留了多个环形缓冲区,但并非多有的日志消息都会发送到默认的环形缓冲区。这里可以采用 logcat -b 命令查看设备的其他缓冲区:

缓冲区

描述

举例

radio

输出通信系统的日志,包含无线装置/电话相关消息

logcat -b radio

events

输出event模块的日志

logcat -b events

main

主日志缓冲区(默认),不包含系统和崩溃日志消息

logcat -b main

system

输出系统日志

logcat -b system

crash

输出崩溃日志

logcat -b crash

all

输出所有缓冲区日志

logcat -b all

default

输出main、system、crash缓冲区日志

logcat -b default

缓冲区模型

logcat命令参数

参数

含义

举例

-b

加载⼀个可使⽤的⽇志缓冲区供查看,⽐如 event和 radio 。默认值是 main

logcat -b system

-B

以二进制文件形式输出日志

-c

清除缓冲区中的全部⽇志并退出(清除完后可以使⽤ -g 查看缓冲区)默认清除 mainsystemcrash

logcat -c;logcat -b all -c

-d

将缓冲区的 log 转存到屏幕中然后退出

logcat -d > log.txt

-f <file>

将 log 输出到指定的⽂件中 < ⽂件名 >. 默认为标准输出( stdout )

logcat -f sdcard/log.txt

-g

打印⽇志缓冲区的大小并退出

logcat -g

-G

设置日志环形缓冲区的大小,可以在结尾处添加 KM

logcat -G 2M

-n <count>

设置⽇志的最⼤数⽬,默认值是 4 ,需要和 -r 选项⼀起使⽤

-r <kbytes>

每时输出⽇志,默认值是 16 ,需要和 -f 选项⼀起使⽤

logcat -f sdcard/log.txt -r 1

-s

输出指定 tag 的日志,相当于过滤器表达式 *:S

logcat -s tag

-S

在输出中包含统计信息,以识别和定位日志垃圾信息发送者

-v <format>

设置输出格式的⽇志消息。默认是短暂的格式。⽀持的格式列表

logcat -v threadtime

-m <count>

输出 行后退出

logcat -m 5

-D

输出各个日志缓冲区之间的分隔线

logcat -D …

-e <expr>

输出正则匹配的日志消息

logcat -e [匹配数据] -m 5

-t <count>

仅输出最新的行数,此选项包括 -d 功能

logcat -t 5

-t '<time>'

输出自指定时间以来的最新行,此选项包括 -d 功能

logcat -t ‘01-26 20:52:41.820’

--pid=<pid>

仅输出来自给定 PID 的日志

logcat --pid=4321

⼀般⻓时间输出 log 的话建议 -f , -n , -r 三个参数连⽤,这样当⼀个⽂件⽇志输出满了之后可以⻢上在另⼀个中进⾏输出。

Android 日志系统为日志消息保留了多个环形缓冲区,但并非多有的日志消息都会发送到默认的环形缓冲区。

logcat -blogcat 命令的一个选项,用于指定查看日志缓冲区。你可以用它来查看不同类型的日志缓冲区。下面是基本用法:

logcat -b events|grep -E "am_proc_start|input_focus.*entering"

基本命令格式:

shCopy Code
adb logcat -b <buffer>

选项说明:

  • main: 默认缓冲区,包含大多数应用程序和系统日志。

  • system: 系统日志缓冲区,包含系统服务和硬件相关日志。

  • events: 事件日志缓冲区,记录系统事件和调试信息。

  • crash: 崩溃日志缓冲区,记录应用崩溃的信息。

示例用法:

  • 查看主缓冲区日志:

shCopy Code
adb logcat -b main
  • 查看系统缓冲区日志:

shCopy Code
adb logcat -b system
  • 查看事件缓冲区日志:

shCopy Code
adb logcat -b events
  • 查看崩溃缓冲区日志:

shCopy Code
adb logcat -b crash

这些选项帮助你根据需要过滤和查看不同类型的日志数据。

logcat格式化输出

参数说明

⽇志消息包含⼀个元数据字段,除了标签和优先级,您可以修改输出显示⼀个特定的元数据字段格式的消息。为此,您使⽤-v 选项来指定⼀个⽀持的输出格式。⼀下为⽀持的格式:

格式

含义

brief

显示优先级 / 标记和过程的 PID 发出的消息(默认格式)

process

只显示PID

tag

只显示优先级/标记

raw

显示原始的⽇志消息,没有其他元数据字段

time

调⽤显示⽇期、时间、优先级 / 标签和过程的 PID 发出消息

thread

旧版格式,显示优先级、PID 以及发出消息的线程的 TID

threadtime

调⽤显示⽇期、时间、优先级、标签遗迹 PID TID线程发出的消息

long

显示所有元数据字段与空⽩⾏和单独的消息

color

使用不同的颜色来显示每个优先级

descriptive

显示日志缓冲区事件说明。此修饰符仅影响事件日志缓冲区消息,不会对其他非二进制文件缓冲区产生任何影响

epoch

显示自 1970 年 1 月 1 日以来的时间(以秒为单位)

uid

如果访问控制允许,则显示 UID 或记录的进程的 Android ID

usec

显示精确到微秒的时间

UTC

显示 UTC 时间

year

将年份添加到显示的时间

zone

将本地时区添加到显示的时间

当 logcat 开始,指定想要输出格式 -v 选项:

[adb] logcat [-v ]
adb logcat –v thread

只能指定⼀个输出格式 -v。

logcat优先级

按日志级别过滤日志:

adb logcat <tag>[:priority]

tag 表示标签;priority 表示输出级别;

日志默认级别是 V,如果错误日志我们选择 E 就可以,Android 的日志分为如下几个优先级(priority):

选项

描述

举例

V

–Verbose(最低优先级)

adb logcat *:V

D

– Debug

adb logcat *:D

I

– Info

adb logcat *:I

W

– Warning

adb logcat *:W

E

– Error

adb logcat *:E

F

– Fatal

adb logcat *:F

S

– Silent

adb logcat *:S

*可以是某个tag,如果没有指明,就表示所有。

  • 30
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值