adb logcat介绍

logcat命令语法:

[adb] logcat [<option>] ... [<filter-spec>] ...
  • adb logcat -c 清除所有以前的日志
  • adb logcat -d 打印日志,且自动退出log模式
  • adb logcat -f 将日志输出到文件中。注意,这个文件保存在设备上,所以需要将日志创建在可写入的地方。比如/sdcard/logs.txt
  • adb logcat -s 设置默认的过滤器, 如 我们想要输出 "System.out" 标签的信息, 就可以使用adb logcat -s System.out
  • adb logcat -t N 查看最后的N条日志
  • adb logcat | grep XXX 管道命令,同Linux

也可以进入adb shell后,运行logcat。与adb logcat 参数一样。

1 日志过滤

每一个输出的Android日志信息都有一个标签和它的优先级。

  • 日志的标签是系统部件原始信息的一个简要的标志。(比如:“View”就 是查看系统的标签).
  • 优先级有下列集中,是按照从低到高顺利排列的:
    • V — Verbose (lowest priority)
    • D — Debug
    • I — Info
    • W — Warning
    • E — Error
    • F — Fatal
    • S — Silent (highest priority, on which 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的中可以得到日志的优先级. 你可以在过滤器中多次写tag:priority 。这些说明都只到空白结束。下面有一个列子,例子表示支持所有的日志信息,除了那些标签 为”ActivityManager”和优先级为”Info”以上的和标签为” MyApp”和优先级为” Debug”以上的。 

adb logcat ActivityManager:I MyApp:D *:S

上面表达式的最后的元素 *:S,是设置所有的标 签为”silent”,所有日志只显示有”View” and “MyApp”的,用 *:S 的另一个用处是 能够确保日志输出的时候是按照过滤器的说明限制的,也让过滤器也作为一项输出到日志中。如果你电脑上运行logcat ,相比在远程adbshell端,你还可以 为环境变量ANDROID_LOG_TAGS :输入一个参数来设置默认的过滤

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

需要注意的是ANDROID_LOG_TAGS 过滤器如果 通过远程shell运行logcat 或 用adb shell logcat 来 运行模拟器/设备不能输出日志。

2 控制日志输出格式

日志信息包括了许多元数据域包括标签和优先级。可以修改日志的输出格式,所以可以显示出特 定的元数据域。可以通过 -v 选项得到格式化输出日志的相关信息.

  • brief — Display priority/tag and PID of originating process (the default format)
  • process — Display PID only
  • tag — Display the priority/tag only.
  • thread — Display process:thread and priority/tag only.
  • raw — Display the raw log message, with no other metadata fields.
  • time — Display the date, invocation time, priority/tag, and PID of the originating process.
  • long — Display all metadata fields and separate messages with a blank lines.

当启动了logcat ,你可以通过-v 选 项来指定输出格式:

[adb] logcat [-v <format>]

下面是用 thread 来产生的日志格式:

adb logcat -v thread

需要注意的是你只能-v 选项来规定输出格式 option。

3. 使用win7 console查看日志有乱码

乱码的原因可能是中文编码不对。可进行如下操作:1. console中输入 chcp 65001 后,切换编码。2. 右键console的标题部分,点击属性。在新弹出的窗口选择【字体】选项卡,然后在下面的字体里选择【Lucida Console】这个字体。到此,可显示正常显示中文。

posted @ 2015-07-09 15:33  myLittleGarden  阅读(601)  评论(0编辑  收藏  举报