您的位置:首页 > 移动开发 > Android开发

Android工具类之LogUtil

2015-09-21 11:49 393 查看
package com.guan.o2o.utils;

import android.util.Log;

/**
* @author Guan
* @file com.guan.o2o.utils
* @date 2015/9/21
* @Version 1.0
*/
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 final 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);
}
}
}


1、只有当 LEVEL常量的值小于或等于对应日志级别值 的时候,才会将日志打印出来。

2、LogUtil.d(“TAG”, “debug log”);

LogUtil.w(“TAG”, “warn log”);

3、需要修改 LEVEL常量的值,就可以自由地控制日志的打印行为了。比如让 LEVEL等于 VERBOSE 就可以把所有的日志都打印出来,让 LEVEL等于 WARN 就可以只 打印警告以上级别的日志,让 LEVEL等于 NOTHING就可以把所有日志都屏蔽掉。

4、使用了这种方法之后,刚才所说的那个问题就不复存在了,你只需要在开发阶段将 LEVEL指定成 VERBOSE,当项目正式上线的时候将 LEVEL指定成 NOTHING就可以了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android