Android中Log機制詳解

來源:互聯網
上載者:User
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"

 

 原文地址

http://stulog.com/?post=212

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.