文章原文:Android logcat介绍
返回知识列表:Android知识列表
写这个文章之前,总感觉,不就是个logcat吗,至于要总结吗。但当用的时候,你会发现,你必须 快速,准确 的写出你想要的命令是多么的困难。所以这个总结是很有必要的。
Android 日志记录系统是系统进程 logd
维护的一组结构化环形缓冲区。这组可用的缓冲区是固定的,并由系统定义。最相关的缓冲区为:main
(用于存储大多数应用日志)、system
(用于存储源自 Android 操作系统的消息)和 crash
(用于存储崩溃日志)。每个日志条目都包含一个优先级(VERBOSE
、DEBUG
、INFO
、WARNING
、ERROR
或 FATAL
)、一个标识日志来源的标记以及实际日志消息。
日志记录系统的主接口是共享库 liblog
及其头文件 <android/log.h>
。所有语言特定的日志记录工具最终都会调用函数 __android_log_write
。默认情况下,它会调用函数 __android_log_logd_logger
,该函数使用套接字将日志条目发送到 logd
。从 API 级别 30 开始,可通过调用 __android_set_log_writer
更改日志记录函数。如需了解详情,请参阅 NDK 文档。
运行 adb logcat
显示的日志要经过四个级别的过滤:
- 编译时过滤:根据编译设置,某些日志可能会从二进制文件中完全移除。例如,可以将 ProGuard 配置为从 Java 代码中移除对
Log.d
的调用。 - 系统属性过滤:
liblog
会查询一组系统属性以确定要发送到logd
的最低严重级别。如果日志具有MyApp
标记,系统会检查以下属性,并且日志应包含最低严重级别的第一个字母(V
、D
、I
、W
、E
或S
以停用所有日志):log.tag.MyApp 举例说明:系统中有property设置为 [log.tag.APM_AudioPolicyManager]:[D]
persist.log.tag.MyApp
log.tag
persist.log.tag
- 应用过滤:如果未设置任何属性,
liblog
会使用__android_log_set_minimum_priority
设置的最低优先级。默认设置为INFO
。 - 显示过滤:
adb logcat
支持其他可减少logd
显示的日志数量的过滤条件。有关详情,请参阅下文。
logcat --help Usage: logcat [options] [filterspecs] options include: | |
-s Set default filter to silent. Equivalent to filterspec '*:S' | 相当于过滤器表达式 '*:S' ;它将所有标记的优先级设为“静默”,并用于放在可添加内容的过滤器表达式列表之前。如需了解详情,请转到介绍过滤日志输出的部分。(在表格的下方) |
-f <file>, --file=<file> Log to file. Default is stdout | 将日志消息输出写入 以后不要用 logcat > debug.log 而应该使用 logcat -f debug.log |
-r <kbytes>, --rotate-kbytes=<kbytes> Rotate log every kbytes. Requires -f option | 每输出 logcat -r 102400 -f debug.log 则,1024 * 100 KB = 100 M, 保存日志文件大小为100M. 指定文件名为debug.log. |
-n <count>, --rotate-count=<count> Sets max number of rotated logs to <count>, default 4 | 将轮替日志的数量上限设置为 结合上一个命令。我们来看。 logcat -r 102400 -n 10 -f debug.log 则,每会循环生成如下日志。 debug.log debug.log.1 debug.log.2 .... debug.log.9 可以想象这整个10个文件,就是一个大的buffer,log在循环的往里放东西。 |
--id=<id> If the signature id for logging to file changes, then clear the fileset and continue | |
-v <format>, --format=<format> | 设置日志消息的输出格式。默认格式为 logcat -v threadtime -f debug.log |
-D, --dividers Print dividers between each log buffer | |
-c, --clear Clear (flush) the entire log and exit if Log to File specified, clear fileset instead | |
-d Dump the log and then exit (don't block) | |
-e <expr>, --regex=<expr> Only print lines where the log message matches <expr> where <expr> is a Perl-compatible regular expression | |
-m <count>, --max-count=<count> Quit after printing <count> lines. This is meant to be paired with --regex, but will work on its own. | |
Paired with --regex and --max-count to let content bypass | |
-t <count> Print only the most recent <count> lines (implies -d) | |
-t '<time>' Print most recent lines since specified time (implies -d) | |
-T <count> Print only the most recent <count> lines (does not imply -d) | |
-T '<time>' Print most recent lines since specified time (not imply -d) count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...' | |
-g, --buffer-size Get the size of the ring buffer. | |
-G <size>, --buffer-size=<size> Set size of log ring buffer, may suffix with K or M. | |
-L, --last Dump logs from prior to last reboot | |
-b <buffer>, --buffer=<buffer> Request alternate ring buffer, 'main','system', 'radio', 'events', 'crash', 'default' or '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 interleaved. Default -b main,system,crash. | |
-B, --binary Output the log in binary. | |
-S, --statistics Output statistics. | |
-p, --prune Print prune white and ~black list. Service is specified as UID, UID/PID or /PID. Weighed for quicker pruning if prefix with ~, otherwise weighed for longevity if unadorned. All other pruning activity is oldest first. Special case ~! represents an automatic quicker pruning for the noisiest UID as determined by the current statistics. | |
-P '<list> ...', --prune='<list> ...' Set prune white and ~black list, using same format as listed above. Must be quoted. | |
--pid=<pid> Only prints 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. | |
filterspecs are a series of
where <tag> is a log component tag (or * for all) and priority is: | |
'*' 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" | |
过滤日志输出
- 日志消息的标记是一个简短的字符串,指示消息所源自的系统组件(例如,“View”表示视图系统)。
- 优先级是以下字符值之一(按照从最低到最高优先级的顺序排列):
-
V
:详细(最低优先级)D
:调试I
:信息W
:警告E
:错误F
:严重错误S
:静默(最高优先级,绝不会输出任何内容)
通过运行 Logcat 并观察每条消息的前两列,您可以获取系统中使用的带有优先级的标记列表,格式为 <priority>/<tag>
。
以下是使用 logcat -v brief output
命令获取的简短 Logcat 输出的示例。它表明消息与优先级“I”和标记“ActivityManager”相关:
I/ActivityManager( 585): Starting activity: Intent { action=android.intent.action...}
如要将日志输出降低到可管理的水平,您可以使用过滤器表达式限制日志输出。通过过滤器表达式,您可以向系统指明您感兴趣的标记/优先级组合,系统会针对指定的标记抑制其他消息。
过滤器表达式采用 tag:priority ...
格式,其中 tag
指示您感兴趣的标记,priority
指示可针对该标记报告的最低优先级。不低于指定优先级的标记的消息会写入日志。您可以在一个过滤器表达式中提供任意数量的 tag:priority
规范。一系列规范使用空格分隔。
以下是一个过滤器表达式的示例,该表达式会抑制除标记为“ActivityManager”、优先级不低于“信息”的日志消息,以及标记为“MyApp”、优先级不低于“调试”的日志消息以外的所有其他日志消息:
adb logcat ActivityManager:I MyApp:D *:S
上述表达式中最后一个元素 *:S
将所有标记的优先级设为“静默”,从而确保系统仅显示标记为“ActivityManager”和“MyApp”的日志消息。使用 *:S
是确保日志输出受限于您已明确指定的过滤器的绝佳方式,它可以让过滤器充当日志输出的“许可名单”。
以下过滤器表达式显示了优先级不低于“警告”的所有标记的所有日志消息:
adb logcat *:W
如果您从开发计算机运行 Logcat(相对于在远程 adb shell 上运行),则也可以通过导出环境变量 ANDROID_LOG_TAGS
的值设置默认过滤器表达式:
export ANDROID_LOG_TAGS="ActivityManager:I MyApp:D *:S"
请注意,如果您从远程 shell 或使用 adb shell logcat
运行 Logcat,系统不会将 ANDROID_LOG_TAGS
过滤器导出到模拟器/设备实例。
控制日志输出格式
除标记和优先级外,日志消息还包含许多元数据字段。您可以修改消息的输出格式,以便它们显示特定的元数据字段。为此,您可以使用 -v
选项,并指定下列某一受支持的输出格式。
brief
:显示优先级、标记以及发出消息的进程的 PID。long
:显示所有元数据字段,并使用空白行分隔消息。process
:仅显示 PID。raw
:显示不包含其他元数据字段的原始日志消息。tag
:仅显示优先级和标记。thread:
:旧版格式,显示优先级、PID 以及发出消息的线程的 TID。threadtime
(默认值):显示日期、调用时间、优先级、标记、PID 以及发出消息的线程的 TID。time
:显示日期、调用时间、优先级、标记以及发出消息的进程的 PID。
启动 Logcat 时,您可以使用 -v
选项指定所需的输出格式:
[adb] logcat [-v <format>]
以下示例显示了如何生成输出格式为 thread
的消息:
adb logcat -v thread
请注意,您只能使用 -v
选项指定一种输出格式,但可以指定任意数量的有意义的修饰符。Logcat 会忽略没有意义的修饰符。
格式修饰符
格式修饰符依据以下一个或多个修饰符的任意组合更改 Logcat 输出。如要指定格式修饰符,请使用 -v
选项,如下所示:
adb logcat -b all -v color -d
每个 Android 日志消息都有一个与之相关联的标记和优先级。您可以将任何格式修饰符与以下任一格式选项进行组合:brief
、long
、process
、raw
、tag
、thread
、threadtime
和 time
。
您可以通过在命令行中输入 logcat -v --help
获取格式修饰符详细信息。
color
:使用不同的颜色来显示每个优先级。descriptive
:显示日志缓冲区事件说明。此修饰符仅影响事件日志缓冲区消息,不会对其他非二进制文件缓冲区产生任何影响。事件说明取自 event-log-tags 数据库。epoch
:显示自 1970 年 1 月 1 日以来的时间(以秒为单位)。monotonic
:显示自上次启动以来的时间(以 CPU 秒为单位)。printable
:确保所有二进制日志记录内容都进行了转义。uid
:如果访问控制允许,则显示 UID 或记录的进程的 Android ID。usec
:显示精确到微秒的时间。UTC
:显示 UTC 时间。year
:将年份添加到显示的时间。zone
:将本地时区添加到显示的时间。
查看备用日志缓冲区
Android 日志记录系统为日志消息保留了多个环形缓冲区,而且并非所有的日志消息都会发送到默认的环形缓冲区。如要查看其他日志消息,您可以使用 -b
选项运行 logcat
命令,以请求查看备用的环形缓冲区。您可以查看下列任意备用缓冲区:
radio
:查看包含无线装置/电话相关消息的缓冲区。events
:查看已经过解译的二进制系统事件缓冲区消息。main
:查看主日志缓冲区(默认),不包含系统和崩溃日志消息。system
:查看系统日志缓冲区(默认)。crash
:查看崩溃日志缓冲区(默认)。all
:查看所有缓冲区。default
:报告main
、system
和crash
缓冲区。
以下是 -b
选项的用法:
[adb] logcat [-b <buffer>]
以下示例显示了如何查看包含无线装置和电话相关消息的日志缓冲区。
adb logcat -b radio
此外,您也可以为要输出的所有缓冲区指定多个 -b
标记,如下所示:
logcat -b main -b radio -b events
您可以指定一个 -b
标记,后跟缓冲区逗号分隔列表,例如:
logcat -b main,radio,events
通过代码记录日志
通过 Log
类,您可以在代码中创建日志条目,而这些条目会显示在 Logcat 工具中。常用的日志记录方法包括:
Log.v(String, String)
(详细)Log.d(String, String)
(调试)Log.i(String, String)
(信息)Log.w(String, String)
(警告)Log.e(String, String)
(错误)
例如,使用以下调用:
Log.i("MyActivity", "MyClass.getView() — get item number $position")
Logcat 输出类似如下:
I/MyActivity( 1557): MyClass.getView() — get item number 1