在Android学习过程中,发现加Android自定的Log.i Log.d 时调试,但是我们的程序完成后,要发布时,想一次性把log的打印一次性关闭,那么就麻烦了!看过这篇文章可能会帮助你! 本人第一次写博客,写得不好请见谅
直接上代码
import android.util.Log;
public class LogUtils {
final static int DEBUG_VERBOSE = 6;
final static int DEBUG_DEBUG = 5;
final static int DEBUG_INFO = 4;
final static int DEBUG_WARN = 3;
final static int DEBUG_ERROR = 2;
final static int DEBUG_ASSERT = 1;
/**
* 只打印大于等于debugLevel的函数
* 跟Linux一样
* 数值越大等级越低
* 6 代表 verbose
* 5 代表 debug
* 4 代表 info
* 3 代表 warn
* 2 代表 error
* 1 代表 assert
* debugLevel >= 6 时表示 打印所有的消息
* debugLevel = 0 时表示不打印所有的消息
*/
static int debugLevel = 6;
private static final String TAG = "CustomLogUtils";
public static void v(String msg){
if (debugLevel >= DEBUG_VERBOSE){
Log.v(TAG, "vvvv" + msg);
}
}
public static void d(String msg){
if (debugLevel >= DEBUG_DEBUG){
Log.d(TAG, "dddd" + msg);
}
}
public static void i(String msg){
if (debugLevel >= DEBUG_INFO){
Log.i(TAG, "iiii" + msg);
}
}
public static void w(String msg){
if (debugLevel >= DEBUG_WARN){
Log.i(TAG, "wwww" + msg);
}
}
public static void e(String msg){
if (debugLevel >= DEBUG_ERROR){
Log.i(TAG, "eeee" + msg);
}
}
public static void a(String msg){
if (debugLevel >= DEBUG_ASSERT){
Log.i(TAG, "aaaa" + msg);
}
}
/**
* 只打印大于等于debugLevel的函数
* 跟Linux一样
* 数值越大等级越低
* 6 代表 verbose
* 5 代表 debug
* 4 代表 info
* 3 代表 warn
* 2 代表 error
* 1 代表 assert
* debugLevel >= 6 时表示 打印所有的消息
* debugLevel = 0 时表示不打印所有的消息
*/
public static int getDebugLevel() {
return debugLevel;
}
/**
* 只打印大于等于debugLevel的函数
* 跟Linux一样
* 数值越大等级越低
* 6 代表 verbose
* 5 代表 debug
* 4 代表 info
* 3 代表 warn
* 2 代表 error
* 1 代表 assert
* debugLevel >= 6 时表示 打印所有的消息
* debugLevel = 0 时表示不打印所有的消息
*/
public static void setDebugLevel(int debugLevel) {
LogUtils.debugLevel = debugLevel;
}
}
代码中有一个debugLevel 这个就是控制是否打印的开关了,当工程完成之后,只要改变debugLevel就可以了,一般改成3就可以了(只打印warn ,error,assert的信息),当然也可以改成0 不打印所有的信息,这个值因个人需要修改。
只打印大于等于debugLevel的函数
* 跟Linux一样
* 数值越大等级越低
* 6 代表 verbose
* 5 代表 debug
* 4 代表 info
* 3 代表 warn
* 2 代表 error
* 1 代表 assert
* debugLevel >= 6 时表示 打印所有的消息
* debugLevel = 0 时表示不打印所有的消息
*/
函数里面的“iiii”,"eeee","wwww"等可以根据自己的需求跟爱好来修改,源码demo在此,可以自行修改,希望对大家有作用