文章来源:http://blog.csdn.net/Android_Tutor/article/details/5081713

Android群里,经常会有人问我,Android Log是怎么用的,今天我就把从网上以及SDK里东拼西凑过来,让大家先一睹为快,希望对大家入门Android Log有一定的帮助。

android.util.Log常用的方法有以下5个:Log.v()Log.d()Log.i()Log.w()以及Log.e()。根据首字母对应Eclipse的调试窗口LogCat的几大选项:VERBOSEDEBUGINFOWARNERROR

1、Log.v 的调试颜色为黑色的。LogCat过滤选项是VERBOSE,任何消息都会输出,这里的v代表verbose啰嗦的意思,平时使用就是Log.v("","")。(v+d+i+w+e)

2、Log.d的输出颜色是蓝色的。LogCat过滤选项是DEBUG,输出debug调试的意思,会输出除Log.v以外的信息。(d+i+w+e)

3、Log.i的输出为绿色LogCat过滤选项是INFO一般提示性的消息information,它不会输出Log.v和Log.d的信息,但会显示i、w和e的信息(i+w+e)

4、Log.w的意思为橙色LogCat过滤选项是WARN可以看作为warning警告,一般需要我们注意优化Android代码,同时选择它后还会输出Log.e的信息。(w+e)

5、Log.e为红色LogCat过滤选项是ERROR可以想到error错误,这里仅显示红色的错误信息,这些错误就需要我们认真的分析,查看栈的信息了。(e)

下面是我做的一个简单的LogDemo(Step By Step):

Step 1:准备工作(打开LogCat视窗).

启动Eclipse,在Window->Show View会出来一个对话框,当我们点击Ok按钮时,会在控制台窗口出现LogCat视窗.如下图:

Step 2:新建一个Android工程,命名为LogDemo.

Step 3:设计UI界面,我们在这里就加了一个Button按钮(点击按钮出现Log日志信息).

Main.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Presse Me Look Log"
/>
</LinearLayout>

Step 4:设计主类LogDemo.java,代码如下:

package com.android.test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public classLogDemoextends Activity {

private static final String ACTIVITY_TAG="LogDemo";
private Button bt;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//通过findViewById找到Button资源
bt = (Button)findViewById(R.id.bt);
//增加事件响应
bt.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
Log.v(LogDemo.ACTIVITY_TAG, "This is Verbose.");
Log.d(LogDemo.ACTIVITY_TAG, "This is Debug.");
Log.i(LogDemo.ACTIVITY_TAG, "This is Information");
Log.w(LogDemo.ACTIVITY_TAG, "This is Warnning.");
Log.e(LogDemo.ACTIVITY_TAG, "This is Error.");
}

});

}

}

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

当我们点击按钮时,会触发事件,在Logcat视窗下有如下效果:

Ok~这个暂时就先讲到这里,有什么不懂的,或者想要源码的,留下你们的Email地址,我会发给你们的,顺便祝大家节日快乐!


更多相关文章

  1. Android(安卓)- 混淆jar包 ProGuard GUI 使用方法
  2. android 中Log - 简单使用
  3. android logcat 命令详解
  4. Android之通过HTTP协议向服务器发送XML数据
  5. LogUtils:一个强大的Android日志管理器,支持对象、List、Map、数组
  6. android中string.xml中%1$s、%1$d等的用法
  7. ORM 框架之greenDAO
  8. Android设置里面默认存储器选项(default write disk)的实现
  9. Android(安卓)OpenGL纹理

随机推荐

  1. android 常见分辨率(mdpi、hdpi 、xhdpi、
  2. android 数据库建立以及自定义ContentPro
  3. 手动root android 模拟器(emulator)详细
  4. Android播放GIF动画
  5. EditText属性
  6. Android(安卓)中文API (70) ―― Bluetooth
  7. Android(安卓)5.0(Lollipop)中的SurfaceT
  8. android Handler浅谈
  9. 一篇文章带你走通 OkHttp+Retrofit+Rxjav
  10. 自定义控件:抽屉SlidingDrawer——wrap_co