[java] view plaincopyprint?
在 android 平臺上搞開發工作,會經常用到一些 Log 輸出調試信息。
眾所周知,android 中有五種類型的 Log , v, d, i, w, e 這裡就不再贅 述 (如果對這些不瞭解的朋友,推薦看 android_Tutor 的博文 /kf/201205/132161.html , 上面講的很詳細)
本文主要講一下如何統一控制 Log 的輸出和關閉。
一般我們會在 debug 的版本中輸出 log,而在 release 版本的產品中關閉 log 的輸出。這一點是如何做到的呢,下面來看代碼。
在你當前工作的工程中建一個新的類文件 Log.java 。這個類文件放在當前工作的包中,(也可以單獨建一個包來放這個類文件)
在下面的類文件中,我們把 android中原始的10個 log 函數重新包瞭一層。定義瞭一個常量 DEBUG ,當 DEBUG 為 true 時才輸出 Log 信息
[java]package com.android.gallery3d.util;
public class Log {
private static final boolean DEBUG = true;
public static void v(String tag, String msg) {
if(DEBUG) {
android.util.Log.v(tag, msg);
}
}
public static void v(String tag, String msg, Throwable tr) {
if(DEBUG) {
android.util.Log.v(tag, msg, tr);
}
}
public static void d(String tag, String msg) {
if(DEBUG) {
android.util.Log.d(tag, msg);
}
}
public static void d(String tag, String msg, Throwable tr) {
if(DEBUG) {
android.util.Log.d(tag, msg, tr);
}
}
public static void i(String tag, String msg) {
if(DEBUG) {
android.util.Log.i(tag, msg);
}
}
public static void i(String tag, String msg, Throwable tr) {
if(DEBUG) {
android.util.Log.i(tag, msg, tr);
}
}
public static void w(String tag, String msg) {
if(DEBUG) {
android.util.Log.w(tag, msg);
}
}
public static void w(String tag, String msg, Throwable tr) {
if(DEBUG) {
android.util.Log.w(tag, msg, tr);
}
}
public static void w(String tag, Throwable tr) {
if(DEBUG) {
android.util.Log.w(tag, tr);
}
}
public static void e(String tag, String msg) {
if(DEBUG) {
android.util.Log.e(tag, msg);
}
}
public static void e(String tag, String msg, Throwable tr) {
if(DEBUG) {
android.util.Log.e(tag, msg, tr);
}
}
}
package com.android.gallery3d.util;
public class Log {
private static final boolean DEBUG = true;
public static void v(String tag, String msg) {
if(DEBUG) {
android.util.Log.v(tag, msg);
}
}
public static void v(String tag, String msg, Throwable tr) {
if(DEBUG) {
android.util.Log.v(tag, msg, tr);
}
}
public static void d(String tag, String msg) {
if(DEBUG) {
android.util.Log.d(tag, msg);
}
}
public static void d(String tag, String msg, Throwable tr) {
if(DEBUG) {
android.util.Log.d(tag, msg, tr);
}
}
public static void i(String tag, String msg) {
if(DEBUG) {
android.util.Log.i(tag, msg);
}
}
public static void i(String tag, String msg, Throwable tr) {
if(DEBUG) {
android.util.Log.i(tag, msg, tr);
}
}
public static void w(String tag, String msg) {
if(DEBUG) {
android.util.Log.w(tag, msg);
}
}
public static void w(String tag, String msg, Throwable tr) {
if(DEBUG) {
android.util.Log.w(tag, msg, tr);
}
}
public static void w(String tag, Throwable tr) {
if(DEBUG) {
android.util.Log.w(tag, tr);
}
}
public static void e(String tag, String msg) {
if(DEBUG) {
android.util.Log.e(tag, msg);
}
}
public static void e(String tag, String msg, Throwable tr) {
if(DEBUG) {
android.util.Log.e(tag, msg, tr);
}
}
}
而當前的包中可以使用 Log.v, Log.i, Log.w, Log.e, Log.d 來打 Log。
在其它的包中要打Log 也很簡單,隻要import 該類名就可以瞭。以上面的類文件為例,隻要在其它包的類文件中 import com.android.gallery3d.util.Log;
就可以使用原來的 Log 函數來打 Log 瞭。
在Release 版本的軟件上將 DEBUG 置為 false 即可關閉 Log 輸出瞭。
摘自 fulinwsuafcie的專欄