Android中Log機制詳解

Android中Log的輸出有以下幾種:

Log.v(String tag, String msg);        //VERBOSE
Log.d(String tag, String msg);       //DEBUG
Log.i(String tag, String msg);        //INFO
Log.w(String tag, String msg);     //WARN
Log.e(String tag, String msg);      //ERROR

以上log的級別依次升高,VERBOSE DEBUG信息應當只存在於開發中,INFO,WARN,ERROR這三種log將出如今發佈版本中。

對於JAVA類中,能夠聲明一個字符串常量TAG,Logcat能夠根據他來區分不一樣的log,例如在WindowsManagerService.java的類中,定義以下所示:
static final Sting TAG = "WindowManager"
須要打log的地方
Log.v(TAG, "Figuring out where to add app window" + client.asBinder() + "(token=" + token + ")");

logcat使用方法以下所示:

logcat [options] [filterspecs]
option "-s" 用來設置過濾器,格式是這樣的 <tag>[:priority]
其中 <tag> 表示log的component, tag (或者使用 * 表示全部) ,priority以下所示:
V Verbose
D Debug
I Info
W Warn
E Error
F Fatal
S Silent

例:
logcat -s *:s  不打任何log
logcat -s WindowMnager:V   <-- 打印WindowManagerService 中 Verbose 信息
若是在eclipse中查看Android log 輸出,也就是logcat信息,能夠 選擇Windows > Show View > Other... > Android > LogCat。 


logcat的參數說明:
Usage: logcat [options] [filterspecs]  
options include:  
  -s              Set default filter to silent.  
                  Like specifying filterspec '*:s'  
  -f <filename>   Log to file. Default to stdout  
  -r [<kbytes>]   Rotate log every kbytes. (16 if unspecified). Requires -f  
  -n <count>      Sets max number of rotated logs to <count>, default 4  
  -v <format>     Sets the log print format, where <format> is one of:  
  
                  brief process tag thread raw time long  
  
  -c              clear (flush) the entire log and exit  
  -d              dump the log and then exit (don't block)  
  -g              get the size of the log's ring buffer and exit  
  -b <buffer>     request alternate ring buffer, defaults to 'main'  
filterspecs are a series of  
  <tag>[:priority]  
  
where <tag> is a log component tag (or * for all) and priority is:  
  V    Verbose  
  D    Debug  
  I    Info  
  W    Warn  
  E    Error  
  F    Fatal  
  S    Silent (supress all output)  
  
'*' means '*:d' and <tag> by itself means <tag>:v  
  
If not specified on the commandline, filterspec is set from ANDROID_LOG_TAG  
If no filterspec is found, filter defaults to '*:I'  
  
If not specified with -v, format is set from ANDROID_PRINTF_LOG  
or defaults to "brief"java

相關文章
相關標籤/搜索