定制自己的日志工具,主要是为了控制日志的打印:开发阶段打印日志,正式版本不打印日志。
这很简单,我们新建一个类LogUtil(日志工具),代码如下:
public class LogUtil {
public static final int VERBOSE = 1;
public static final int DEBUG = 2;
public static final int INFO = 3;
public static final int WARN = 4;
public static final int ERROR = 5;
public static final int NOTHING = 6;
public static int level = VERBOSE;
public static void v(String tag, String msg) {
if (level <= VERBOSE) {
Log.v(tag, msg);
}
}
public static void d(String tag, String msg) {
if (level <= DEBUG) {
Log.d(tag, msg);
}
}
public static void i(String tag, String msg) {
if (level <= INFO) {
Log.i(tag, msg);
}
}
public static void w(String tag, String msg) {
if (level <= WARN) {
Log.w(tag, msg);
}
}
public static void e(String tag, String msg) {
if (level <= ERROR) {
Log.e(tag, msg);
}
}
}
代码很清晰,也很简单,只要我们再项目开发的时候将level指定为VERBOSE,在正式版中将level指定为NOTHING,上面提到的目标就达成啦!