俱乐部关于我们业务联系
当前位置:Android开发首页 >> Android开发文档 >> 如何在Android SDK程序中输出日志此版投稿 | 网友留言 | 高级搜索 RSS 订阅此版块
如何在Android SDK程序中输出日志
2008-05-29 14:00:39  作者:  来源:互联网  浏览次数:4  文字大小:  
简介:Android SDK下, 如何在程序中输出日志 以及如何查看日志. 闲话少说,直接进入正题 在程序中输出日志, 使用 android.util.Log 类. 该类提供了若干静态方法 Log.v(String tag, String msg); Log.d(String tag, ...

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)的日志信息.

示例代码如下:

代码
  1. package com.zijun;   
  2.   
  3. import android.app.Activity;   
  4. import android.content.Context;   
  5. import android.graphics.Canvas;   
  6. import android.os.Bundle;   
  7. import android.util.Log;   
  8. import android.view.MotionEvent;   
  9. import android.view.View;   
  10.   
  11. public class MyAndroid extends Activity {   
  12.        
  13.     protected static final String ACTIVITY_TAG="MyAndroid";   
  14.        
  15.     @Override  
  16.     protected void onCreate(Bundle icicle) {   
  17.         super.onCreate(icicle);   
  18.         setContentView(new MyView(this));   
  19.     }   
  20.     public class MyView extends View {   
  21.         public MyView(Context c) {   
  22.             super(c);   
  23.         }   
  24.         @Override  
  25.         protected void onDraw(Canvas canvas) {   
  26.     
  27.         }   
  28.         @Override  
  29.         public boolean onMotionEvent(MotionEvent event) {   
  30.             Log.i(MyAndroid.ACTIVITY_TAG, "=============================");   
  31.                
  32.             Log.d(MyAndroid.ACTIVITY_TAG, "Haha , this is a DEBUG of MyAndroid. ");   
  33.             Log.i(MyAndroid.ACTIVITY_TAG, "Haha , this is a INFO of MyAndroid. ");   
  34.             Log.w(MyAndroid.ACTIVITY_TAG, "Haha , this is a WARNING of MyAndroid. ");   
  35.   
  36.             return true;   
  37.         }   
  38.            
  39.     }   
  40.   
  41. }   

以上程序运行后, 在命令行执行 adb logcat -s MyAndroid:I
然后在手机模拟器的屏幕上 点击 拖动鼠标 就能看到相应的日志信息.

在eclipse 里选择 show view 然后选择 LogCat
可以更方便的查看, 而不用一定非要到控制台

附 logcat的参数说明,这个说明比较难找 我是无意间发现
adb logcat .....

代码
  1. Usage: logcat [options] [filterspecs]   
  2. options include:   
  3.   -s              Set default filter to silent.   
  4.                   Like specifying filterspec '*:s'   
  5.   -f <filename>   Log to file. Default to stdout   
  6.   -r [<kbytes>]   Rotate log every kbytes. (16 if unspecified). Requires -f   
  7.   -n <count>      Sets max number of rotated logs to <count>, default 4  
  8.   -v <format>     Sets the log print format, where <format> is one of:   
  9.   
  10.                   brief process tag thread raw time long  
  11.   
  12.   -c              clear (flush) the entire log and exit   
  13.   -d              dump the log and then exit (don't block)   
  14.   -g              get the size of the log's ring buffer and exit   
  15.   -b <buffer>     request alternate ring buffer, defaults to 'main'   
  16. filterspecs are a series of   
  17.   <tag>[:priority]   
  18.   
  19. where <tag> is a log component tag (or * for all) and priority is:   
  20.   V    Verbose   
  21.   D    Debug   
  22.   I    Info   
  23.   W    Warn   
  24.   E    Error   
  25.   F    Fatal   
  26.   S    Silent (supress all output)   
  27.   
  28. '*' means '*:d' and <tag> by itself means <tag>:v   
  29.   
  30. If not specified on the commandline, filterspec is set from ANDROID_LOG_TAG   
  31. If no filterspec is found, filter defaults to '*:I'   
  32.   
  33. If not specified with -v, format is set from ANDROID_PRINTF_LOG   
  34. or defaults to "brief"

热点更多...

频道推荐图片与文章

推荐文章

热点文章

栏目最新专题