转自:http://tech.ddvip.com/2010-07/1279268692157659.html
在Android群里,经常会有人问我,Android Log是怎么用的,今天我就把从网上以及SDK里东拼西凑过来,让大家先一睹为快,希望对大家入门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错误,这里仅显示红色的错误信息,这些错误就需要我们认真的分析,查看栈的信息了。
下面是我做的一个简单的LogDemo(Step By Step):
Step 1:准备工作(打开LogCat视窗).
启动Eclipse,在Window->Show View会出来一个对话框,当我们点击Ok按钮时,会在控制台窗口出现LogCat视窗.如下图:
Step 2:新建一个Android工程,命名为LogDemo.
Step 3:设计UI界面,我们在这里就加了一个Button按钮(点击按钮出现Log日志信息).
Main.xml代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<? xml version = "1.0" encoding = "utf-8" ?>
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,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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 class LogDemo extends 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地址,我会发给你们的,顺便祝大家节日快乐!
相关推荐
"Android 中级教程之 Log 图文详解" Android 中的 Log 机制是 Android 开发中非常重要的一部分,它允许开发者在应用程序中输出日志信息,以便于调试和优化代码。Log 机制提供了多种输出级别,包括 VERBOSE、DEBUG...
在Android群里,经常会有人问我,Android Log是怎么用的,今天我就把从网上以及SDK里东拼西凑过来,让大家先一睹为快,希望对大家入门Android Log有一定的帮助.android.util.Log常用的方法有以下5个:Log.v() Log....
在ActivityDemo中,我们可以在每个生命周期方法中添加Log语句,运行应用后查看日志,这样就能清楚地看到这些方法的执行顺序。 源码分析方面,Android框架层的ActivityManagerService是负责管理所有Activity的系统...
### Android Activity 生命周期详解 #### 一、概述 在Android开发中,`Activity`是四大组件之一,用于构建用户界面,并负责与用户的交互。一个`Activity`通常对应着一个屏幕,当用户在一个应用的不同屏幕间导航时...
Log图文详解(Log.v,Log.d,Log.i,Log.w,Log.e)!.doc、 SeekBar(SeekBar)的使用.doc、 Spinner(Spinner)的使用.doc、 Tab与TabHost.pdf、 按钮(Button)的使用.doc、 单选框(RadioButton)的使用.doc、 等级条(RatingBar...
android ListView详解.doc Android 中几个常用属性的设置.doc android 读取文件内容操作.doc Android 选项卡效果.doc Android中 信息的输出——Log.v的使用.doc Android手机连接电脑详细图文教程.doc android...
android ListView详解.doc Android 中几个常用属性的设置.doc android 读取文件内容操作.doc Android 选项卡效果.doc Android中 信息的输出——Log.v的使用.doc Android手机连接电脑详细图文教程.doc android横竖屏...
在每个生命周期方法中使用`Log`记录日志,然后观察`Logcat`窗口,追踪Activity在不同操作下的状态变化。例如,当你启动应用时,会依次看到`onCreate()`、`onStart()`和`onResume()`的日志;当切换到其他应用或打开...
《EasyAndroid:一站式Android开发工具库详解》 在Android应用开发过程中,为了提高效率和代码质量,开发者常常会依赖一些工具库。EasyAndroid就是这样一款专为Android开发者设计的开源工具集合,它包含了丰富的...