Android Debug Bridge(adb) 与 LogCat

来源:互联网 发布:手机扫描搜索不到网络 编辑:程序博客网 时间:2024/05/21 07:58


adb是多种用途的工具,该工具可以帮助你你管理设备或模拟器的状态。在android中,adb可以调动LogCat Show View,用来替代Console (android的后台信息显示:包括System.out.println()、e.printStackTrace()不会在console中显示,而需要在LogCat中显示)。Logcat用于观察调试内容,LogCat不支持中文,但是其过滤器功能很好用,System.out.println()是以I级别显示在LogCat中的
Log.v的调试颜色为黑色 的,任何消息都会输出;
Log.d的输出颜色是蓝色的 ,仅输出debug,但他会输出上层的信息,过滤通过DDMS的Logcat标签来选择。
Log.i的输出为绿色 ,一般提示性的消息information,它不会输出Log.v和Log.d的信息,但会显示i、w和e的信息。
Log.w的意思为橙色 ,需要我们注意优化Android代码,同时选择它后还会输出Log.e的信息。
Log.e为红色 ,这些错误就需要我们认真的分析,查看栈的信息了。


在android程序中输出日志,使用android.util.Log 类。该类提供了若干静态方法

Log.v(String tag, String msg);
Log.d(String tag, String msg);
Log.i(String tag, String msg);
Log.w(String tag, String msg);
Log.e(String tag, String msg);

分别对应 Verbose, Debug, Info, Warning, Error。其中tag是一个标识, 可以是任意字符串, 通常可以使用类名+方法名, 主要是用来在查看日志时提供一个筛选条件 。android规范建议VERBOSE,DEBUG信息应当只存在于开发中,最终版本只可以包含INFO, WARN,ERROR这三种日志信息。

日志显示命令行:adb logcat(Run As --> Run Configurations --> Target --> Additional Emulator Command Line Options)。
当执行adb logcat后会以tail方式实时显示出所有的日志信息. 这时候我们通常需要对信息进行过滤,来显示我们需要的信息,这时候我们指定的TAG就派上了用场.

如果在代码中或者是framework层中加入下面这行代码(红色标示)

Android <wbr>Debug <wbr>Bridge(adb) <wbr>与 <wbr>LogCat

此时如果我想看到这个日志 

命令行中输入:adb logcat -s TagRecord:v
这时将只显示TAG为
TagRecord
, 任何消息的日志信息. 如图

Android <wbr>Debug <wbr>Bridge(adb) <wbr>与 <wbr>LogCat

如果在应用程序中用了这段代码

Android <wbr>Debug <wbr>Bridge(adb) <wbr>与 <wbr>LogCat

Android <wbr>Debug <wbr>Bridge(adb) <wbr>与 <wbr>LogCat
此时要看到他们的日志,则在命令行中输入

adb logcat -s System.out:v

如下图所示

Android <wbr>Debug <wbr>Bridge(adb) <wbr>与 <wbr>LogCat


LogCat参明 数说 (感谢javaeye fins网友提供)

Java代码  收藏代码Android <wbr>Debug <wbr>Bridge(adb) <wbr>与 <wbr>LogCat
  1. <span style="font-size: small;">Usage: logcat [options] [filterspecs]     
  2. options include:     
  3.   -s              Set default filter to silent.     
  4.                   Like specifying filterspec '*:s'    
  5.   -f <filename>   Log to file. Default to stdout     
  6.   -r [<kbytes>]   Rotate log every kbytes. (16 if unspecified). Requires -f     
  7.   -n <count>      Sets max number of rotated logs to <count>, default 4    
  8.   -v <format>     Sets the log print format, where <format> is one of:     
  9.     
  10.                   brief process tag thread raw time long    
  11.     
  12.   -c              clear (flush) the entire log and exit     
  13.   -d              dump the log and then exit (don't block)     
  14.   -g              get the size of the log's ring buffer and exit     
  15.   -b <buffer>     request alternate ring buffer, defaults to 'main'    
  16. filterspecs are a series of     
  17.   <tag>[:priority]     
  18.     
  19. where <tag> is a log component tag (or * for all) and priority is:     
  20.   V    Verbose     
  21.   D    Debug     
  22.   I    Info     
  23.   W    Warn     
  24.   E    Error     
  25.   F    Fatal     
  26.   S    Silent (supress all output)     
  27.     
  28. '*' means '*:d' and <tag> by itself means <tag>:v     
  29.     
  30. If not specified on the commandline, filterspec is set from ANDROID_LOG_TAG     
  31. If no filterspec is found, filter defaults to '*:I'    
  32.     
  33. If not specified with -v, format is set from ANDROID_PRINTF_LOG     
  34. or defaults to "brief"  </span> 

http://blog.sina.com.cn/s/blog_4de067e40100popb.html