[轉]Log圖文詳解(Log.v,Log.d,Log.i,Log.w,Log.e)的用法

來源:互聯網
上載者:User

標籤:

轉自:http://blog.csdn.net/zuolongsnail/article/details/6327129

在調試代碼的時候我們需要查看調試資訊,那我們就需要用Android Log類。

android.util.Log常用的方法有以下5個:Log.v() Log.d() Log.i() Log.w() 以及 Log.e() 。根據首字母對應VERBOSE,DEBUG,INFO, WARN,ERROR。

1、Log.v 的調試顏色為黑色的,任何訊息都會輸出,這裡的v代表verbose囉嗦的意思,平時使用就是Log.v("","");

2、Log.d的輸出顏色是藍色的,僅輸出debug調試的意思,但他會輸出上層的資訊,過濾起來可以通過DDMS的Logcat標籤來選擇.

3、Log.i的輸出為綠色,一般提示性的訊息information,它不會輸出Log.v和Log.d的資訊,但會顯示i、w和e的資訊

4、Log.w的意思為橙色,可以看作為warning警告,一般需要我們注意最佳化Android代碼,同時選擇它後還會輸出Log.e的資訊。

5、Log.e為紅色,可以想到error錯誤,這裡僅顯示紅色的錯誤資訊,這些錯誤就需要我們認真的分析,查看棧的資訊了。

注意:不同的列印方法在使用時都是某個方法帶上(String tag, String msg)參數,tag表示的是列印資訊的標籤,msg表示的是需要列印的資訊。

下面是我做的一個簡單的LogDemo(Step By Step):

Step 1:準備工作(開啟LogCat視窗).

 啟動Eclipse,在Window->Show View會出來一個對話方塊,當我們點擊Ok按鈕時,會在控制台視窗出現LogCat視窗.如:

Step 2:建立一個Android工程,命名為LogDemo.

Step 3:設計UI介面,我們在這裡就加了一個Button按鈕(點擊按鈕出現Log日誌資訊).

Main.xml代碼如下:

 

[xhtml] view plaincopy 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     />  
  12. <Button  
  13.  android:id="@+id/bt"  
  14.  android:layout_width="wrap_content"  
  15.  android:layout_height="wrap_content"  
  16.  android:text="Presse Me Look Log"  
  17. />  
  18. </LinearLayout>  

 

Step 4:設計主類LogDemo.java,代碼如下:

 

[java] view plaincopy 
  1. public class LogDemo extends Activity {  
  2.     
  3.  private static final String ACTIVITY_TAG="LogDemo";  
  4.  private Button bt;  
  5.     public void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.         //通過findViewById找到Button資源  
  9.         bt = (Button)findViewById(R.id.bt);  
  10.         //增加事件響應  
  11.         bt.setOnClickListener(new Button.OnClickListener(){  
  12.     @Override  
  13.    public void onClick(View v) {  
  14.     Log.v(LogDemo.ACTIVITY_TAG, "This is Verbose.");  
  15.     Log.d(LogDemo.ACTIVITY_TAG, "This is Debug.");  
  16.     Log.i(LogDemo.ACTIVITY_TAG, "This is Information");  
  17.     Log.w(LogDemo.ACTIVITY_TAG, "This is Warnning.");  
  18.     Log.e(LogDemo.ACTIVITY_TAG, "This is Error.");  
  19.    }  
  20.            
  21.         });  
  22.     }  
  23.           
  24. }  

 

Step 5:運行LogDemo工程,效果如下:

當我們點擊按鈕時,會觸發事件,在Logcat視窗下有如下效果:

本文轉自http://blog.csdn.net/Android_Tutor/archive/2009/12/26/5081713.aspx

[轉]Log圖文詳解(Log.v,Log.d,Log.i,Log.w,Log.e)的用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.