Android SDK下, 如何在程序中输出日志 以及如何查看日志.
2013-09-27 15:34
357 查看
Android SDK下, 如何在程序中输出日志 以及如何查看日志.
闲话少说,直接进入正题
在程序中输出日志, 使用 android.util.Log 类.
该类提供了若干静态方法
Log.v(String tag, String msg);
Log.d(String tag, String msg);
Log.i(String tag, String msg);
Log.w(String tag, String msg);
Log.e(String tag, String msg);
分别对应 Verbose, Debug, Info, Warning,Error.
tag是一个标识,可以是任意字符串,通常可以使用类名+方法名, 主要是用来在查看日志时提供一个筛选条件.
程序运行后 并不会在 ide的控制台内输出任何信息.
如果要后查看日志 请使用
adb logcat
关于adb的更多信息请查看官方网站.
当执行 adb logcat 后会以tail方式实时显示出所有的日志信息.
这时候我们通常需要对信息进行过滤,来显示我们需要的信息, 这时候我们指定的 tag就派上了用场.
adb logcat -s MyAndroid:I
这时将只显示tag为MyAndroid,级别为I或级别高于I(Warning,Error)的日志信息.
示例代码如下:
Java代码
package com.zijun;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class MyAndroid extends Activity {
protected static final String ACTIVITY_TAG="MyAndroid";
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(new MyView(this));
}
public class MyView extends View {
public MyView(Context c) {
super(c);
}
@Override
protected void onDraw(Canvas canvas) {
}
@Override
public boolean onMotionEvent(MotionEvent event) {
Log.i(MyAndroid.ACTIVITY_TAG, "=============================");
Log.d(MyAndroid.ACTIVITY_TAG, "Haha , this is a DEBUG of MyAndroid. ");
Log.i(MyAndroid.ACTIVITY_TAG, "Haha , this is a INFO of MyAndroid. ");
Log.w(MyAndroid.ACTIVITY_TAG, "Haha , this is a WARNING of MyAndroid. ");
return true;
}
}
}
以上程序运行后, 在命令行执行 adb logcat -s MyAndroid:I
然后在手机模拟器的屏幕上 点击 拖动鼠标 就能看到相应的日志信息.
附
logcat的参数说明,这个说明比较难找 我是无意间发现的
adb logcat .....
Java代码
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"
在eclipse 里选择 show view 然后选择 LogCat
可以更方便的查看, 而不用一定非要到控制台
转载地址:http://www.iteye.com/topic/141021
作者:fins(来自iteye的资深会员)
闲话少说,直接进入正题
在程序中输出日志, 使用 android.util.Log 类.
该类提供了若干静态方法
Log.v(String tag, String msg);
Log.d(String tag, String msg);
Log.i(String tag, String msg);
Log.w(String tag, String msg);
Log.e(String tag, String msg);
分别对应 Verbose, Debug, Info, Warning,Error.
tag是一个标识,可以是任意字符串,通常可以使用类名+方法名, 主要是用来在查看日志时提供一个筛选条件.
程序运行后 并不会在 ide的控制台内输出任何信息.
如果要后查看日志 请使用
adb logcat
关于adb的更多信息请查看官方网站.
当执行 adb logcat 后会以tail方式实时显示出所有的日志信息.
这时候我们通常需要对信息进行过滤,来显示我们需要的信息, 这时候我们指定的 tag就派上了用场.
adb logcat -s MyAndroid:I
这时将只显示tag为MyAndroid,级别为I或级别高于I(Warning,Error)的日志信息.
示例代码如下:
Java代码
package com.zijun;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class MyAndroid extends Activity {
protected static final String ACTIVITY_TAG="MyAndroid";
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(new MyView(this));
}
public class MyView extends View {
public MyView(Context c) {
super(c);
}
@Override
protected void onDraw(Canvas canvas) {
}
@Override
public boolean onMotionEvent(MotionEvent event) {
Log.i(MyAndroid.ACTIVITY_TAG, "=============================");
Log.d(MyAndroid.ACTIVITY_TAG, "Haha , this is a DEBUG of MyAndroid. ");
Log.i(MyAndroid.ACTIVITY_TAG, "Haha , this is a INFO of MyAndroid. ");
Log.w(MyAndroid.ACTIVITY_TAG, "Haha , this is a WARNING of MyAndroid. ");
return true;
}
}
}
以上程序运行后, 在命令行执行 adb logcat -s MyAndroid:I
然后在手机模拟器的屏幕上 点击 拖动鼠标 就能看到相应的日志信息.
附
logcat的参数说明,这个说明比较难找 我是无意间发现的
adb logcat .....
Java代码
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"
在eclipse 里选择 show view 然后选择 LogCat
可以更方便的查看, 而不用一定非要到控制台
转载地址:http://www.iteye.com/topic/141021
作者:fins(来自iteye的资深会员)
相关文章推荐
- Android SDK下, 如何在程序中输出日志 以及如何查看日志.
- Android SDK开发中,如何在程序中输出日志查看日志
- 如何查看MapReduce执行的程序中的输出日志
- 如何在Android SDK 下查看应用程序输出日志的方法
- 如何查看端口被是否被占用,以及被哪个程序占用
- Hadoop的nodemanager老是启动就挂掉解决方法以及如何查看hadoop日志
- 详述Linux计划任务的 at和crontab的命令 以及如何查看crontab日志
- 控制台程序 如何查看所有的输出结果
- 怎样在Android SDK 下查看应用程序输出日志的方法
- Android SDK 下查看应用程序输出日志的方法
- Ubuntu 如何查看端口使用情况以及停止使用该端口号的程序
- 如何查看hadoop程序中自己添加的日志
- Druid基本使用配置以及如何查看sql信息监控日志
- 客户端软件成品 该如何查看错误/异常(弹出/生成日志)
- 如何设置ofbiz的日志输出级别
- linux查看端口以及端口的监听程序
- SQL Server-聚焦事务对本地变量、临时表、表变量影响以及日志文件存满时如何收缩(三十一)
- Android 如何在Eclipse中查看Android API源码以及support包源码
- 关于端口以及如何查看端口
- shell小工具查看Hadoop中System.out输出的日志