浅谈Android系统开发中LOG的使用

来源:互联网 发布:mac虚拟机连不上网 编辑:程序博客网 时间:2024/06/06 14:04

Android系统中的Java日志接口。Android系统在Frameworks层中定义了Log接口(frameworks/base/core/java/android/util/Log.java):

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ................................................  
  2.   
  3. public final class Log {  
  4.   
  5. ................................................  
  6.   
  7.     /** 
  8.      * Priority constant for the println method; use Log.v. 
  9.          */  
  10.     public static final int VERBOSE = 2;  
  11.   
  12.     /** 
  13.      * Priority constant for the println method; use Log.d. 
  14.          */  
  15.     public static final int DEBUG = 3;  
  16.   
  17.     /** 
  18.      * Priority constant for the println method; use Log.i. 
  19.          */  
  20.     public static final int INFO = 4;  
  21.   
  22.     /** 
  23.      * Priority constant for the println method; use Log.w. 
  24.          */  
  25.     public static final int WARN = 5;  
  26.   
  27.     /** 
  28.      * Priority constant for the println method; use Log.e. 
  29.          */  
  30.     public static final int ERROR = 6;  
  31.   
  32.     /** 
  33.      * Priority constant for the println method. 
  34.          */  
  35.     public static final int ASSERT = 7;  
  36.   
  37. .....................................................  
  38.   
  39.     public static int v(String tag, String msg) {  
  40.         return println_native(LOG_ID_MAIN, VERBOSE, tag, msg);  
  41.     }  
  42.   
  43.     public static int v(String tag, String msg, Throwable tr) {  
  44.         return println_native(LOG_ID_MAIN, VERBOSE, tag, msg + '\n' + getStackTraceString(tr));  
  45.     }  
  46.   
  47.     public static int d(String tag, String msg) {  
  48.         return println_native(LOG_ID_MAIN, DEBUG, tag, msg);  
  49.     }  
  50.   
  51.     public static int d(String tag, String msg, Throwable tr) {  
  52.         return println_native(LOG_ID_MAIN, DEBUG, tag, msg + '\n' + getStackTraceString(tr));  
  53.     }  
  54.   
  55.     public static int i(String tag, String msg) {  
  56.         return println_native(LOG_ID_MAIN, INFO, tag, msg);  
  57.     }  
  58.   
  59.     public static int i(String tag, String msg, Throwable tr) {  
  60.         return println_native(LOG_ID_MAIN, INFO, tag, msg + '\n' + getStackTraceString(tr));  
  61.     }  
  62.   
  63.     public static int w(String tag, String msg) {  
  64.         return println_native(LOG_ID_MAIN, WARN, tag, msg);  
  65.     }  
  66.   
  67.     public static int w(String tag, String msg, Throwable tr) {  
  68.         return println_native(LOG_ID_MAIN, WARN, tag, msg + '\n' + getStackTraceString(tr));  
  69.     }  
  70.   
  71.     public static int w(String tag, Throwable tr) {  
  72.         return println_native(LOG_ID_MAIN, WARN, tag, getStackTraceString(tr));  
  73.     }  
  74.       
  75.     public static int e(String tag, String msg) {  
  76.         return println_native(LOG_ID_MAIN, ERROR, tag, msg);  
  77.     }  
  78.   
  79.     public static int e(String tag, String msg, Throwable tr) {  
  80.         return println_native(LOG_ID_MAIN, ERROR, tag, msg + '\n' + getStackTraceString(tr));  
  81.     }  
  82.   
  83. ..................................................................  
  84.   
  85.     /**@hide */ public static native int println_native(int bufID,  
  86.         int priority, String tag, String msg);  
  87. }  

        因此,如果要使用Java日志接口,只要在类中定义的LOG_TAG常量和引用android.util.Log就可以了:

        private static final String LOG_TAG = "MY_LOG_TAG";

        Log.i(LOG_TAG, "This is the log printed by Log.i in android user space.");

        要查看这些LOG的输出,可以配合logcat工具。如果是在Eclipse环境下运行模拟器,并且安装了Android插件,那么,很简单,直接在Eclipse就可以查看了:

        

0 0
原创粉丝点击