Android日志系统提供了记录和查看系统调试信息的功能。日志都是从各种软件和一些系统的缓冲区中记录下来的,缓冲区可以通过 logcat
使用logcat命令
你可以用 logcat
[adb] logcat [<option>] ... [<filter-spec>] ...
请查看Listingof logcat CommandOptions
你也可以在你的电脑或运行在模拟器/设备上的远程adbshell端来使用logcat
命 令,也可以在你的电脑上查看日志输出。
$ adb logcat
你也这样使用:
# logcat
过滤 日志输出
每一个输出的Android日志信息都有一个标签和它的优先级.
- 日志的标签是系统部件原始信息的一个简要的标志。(比如:“View”就 是查看系统的标签).
- 优先级有下列集中,是按照从低到高顺利排列的:
V
— Verbose (lowestpriority) D
— Debug I
— Info W
— Warning E
— Error F
— Fatal S
— Silent (highest priority, onwhich nothing is ever printed)
在运行logcat的时候在前两列的信息中你就可以看到 logcat
<priority>/<tag>
下面是一个logcat输出的例子,它的优先级就似乎I,标签就是ActivityManage:
I/ActivityManager( 585): Starting activity: Intent { action=android.intent.action...}
为了让日志输出能体现管理的级别,你还可以用过滤器来控制日志输出,过滤器可以帮助你描述 系统的标签等级.
过滤器语句按照下面的格式描tag:priority...
tag
priority
tag:priority
这些说明都只到空白结束。下面有一个列子,例子表示支持所有的日志信息,除了那些标签为”ActivityManager”和优先级为”Info”以上的和标签为” MyApp”和优先级为” Debug”以上的。小等级,优先权报告为tag.
adb logcat ActivityManager:I MyApp:D *:S
上面表达式的最后的元素 *:S
*:S
下面的过滤语句指显示优先级为warning或更高的日志信息:
adb logcat *:W
如果你电脑上运行logcat
ANDROID_LOG_TAGS
export ANDROID_LOG_TAGS="ActivityManager:I MyApp:D *:S"
需要注意的是ANDROID_LOG_TAGS
logcat
adb shell logcat
控制 日志输出格式
日志信息包括了许多元数据域包括标签和优先级。可以修改日志的输出格式,所以可以显示出特定的元数据域。可以通过 -v
brief
— Display priority/tag andPID of originating process (the default format). process
— Display PID only. tag
— Display the priority/tagonly. thread
— Display process:threadand priority/tag only. raw
— Display the raw logmessage, with no other metadata fields. time
— Display the date,invocation time, priority/tag, and PID of the originatingprocess. long
— Display all metadatafields and separate messages with a blank lines.
当启动了logcat
-v
[adb] logcat [-v <format>]
下面是用 thread
adb logcat -v thread
需要注意的是你只能-v
查看 可用日志缓冲区
Android日志系统有循环缓冲区,并不是所有的日志系统都有默认循环缓冲区。为了得到日志信息,你需要通过-b
logcat
radio
— 查看缓冲区的相关的信息. events
— 查看和事件相关的的缓冲区. main
— 查看主要的日志缓冲区
-b
[adb] logcat [-b <buffer>]
下面的例子表示怎么查看日志缓冲区包含radio 和telephony信息:
adb logcat -b radio
查看 stdout 和stderr
在默认状态下,Android系统有stdout
stderr
System.out
和System.err
/dev/null
stdout
stderr
通过这种方法指定输出的路径,停止运行的模拟器/设备,然后通过用setprop
$ adb shell stop $ adb shell setprop log.redirect-stdio true $ adb shell start
系统直到你关闭模拟器/设备前设置会一直保留,可以通过添加/data/local.prop
Logcat命令列表
Option | Description |
---|---|
-b | 加载一个可使用的日志缓冲区供查看,比如event radio main |
-c | 清楚屏幕上的日志. |
-d | 输出日志到屏幕上. |
-f | 指定输出日志信息的<filename> stdout |
-g | 输出指定的日志缓冲区,输出后退出. |
-n | 设置日志的最大数目<count> -r |
-r | 每<kbytes> -f |
-s | 设置默认的过滤级别为silent. |
-v | 设置日志输入格式,默认的是brief |