Android开发经验总结篇

详细打印日志类

2018-05-02  本文已影响0人  zhengLH
详细打印日志图.png

【LogUtil 类】

  /**
 * @Author: 淘跑
 * @Data: 2018/1/29 10:32
 * @Use: LOG工具类
 */
public class LogUtils {
/** 日志输出级别NONE */
public static final int LEVEL_NONE = 0;
/** 日志输出级别E */
public static final int LEVEL_ERROR =1;
/** 日志输出级别W */
public static final int LEVEL_WARN = 2;
/** 日志输出级别I */
public static final int LEVEL_INFO = 3;
/** 日志输出级别D */
public static final int LEVEL_DEBUG = 4;
/** 日志输出级别f */
public static final int LEVEL_F = 5;
/** 日志输出级别V */
public static final int LEVEL_VERBOSE = 6;

/** 日志输出时的TAG */
private static String mTag = "TaoPao";
/** 是否允许输出log */
private static int mDebuggable = LEVEL_VERBOSE;

/** 以级别为 d 的形式输出LOG */
public static void v(String msg) {
    if (mDebuggable >= LEVEL_VERBOSE) {
        Log.v(mTag, msg);
    }
}

/** 以级别为 d 的形式输出LOG */
public static void d(String msg) {
    if (mDebuggable >= LEVEL_DEBUG) {
        Log.d(mTag, msg);
    }
}

/** 以级别为 i 的形式输出LOG */
public static void i(String msg) {
    if (mDebuggable >= LEVEL_INFO) {
        Log.i(mTag, msg);
    }
}

/** 以级别为 w 的形式输出LOG */
public static void w(String msg) {
    if (mDebuggable >= LEVEL_WARN) {
        Log.w(mTag, msg);
    }
}

/** 以级别为 w 的形式输出Throwable */
public static void w(Throwable tr) {
    if (mDebuggable >= LEVEL_WARN) {
        Log.w(mTag, "", tr);
    }
}

/** 以级别为 w 的形式输出LOG信息和Throwable */
public static void w(String msg, Throwable tr) {
    if (mDebuggable >= LEVEL_WARN && null != msg) {
        Log.w(mTag, msg, tr);
    }
}

/** 以级别为 e 的形式输出LOG */
public static void e(String msg) {
    if (mDebuggable >= LEVEL_ERROR) {
        Log.e(mTag, msg);
    }
}

/** 以级别为 e 的形式输出Throwable */
public static void e(Throwable tr) {
    if (mDebuggable >= LEVEL_ERROR) {
        Log.e(mTag, "", tr);
    }
}

/** 以级别为 e 的形式输出LOG信息和Throwable */
public static void e(String msg, Throwable tr) {
    if (mDebuggable >= LEVEL_ERROR && null != msg) {
        Log.e(mTag, msg, tr);
    }
}


/** 以级别为 f 的形式输出LOG */
public static void f(String msg) {
    if (mDebuggable >= LEVEL_F) {
        Log.e("LoggingInterceptor->Log", msg);
    }
}


public static final String LINE_SEPARATOR = System.getProperty("line.separator");

/**
 * log拦截器打印log 格式化json字符串 并打印在控制台
 * @param msg json字符串
 * @param headString 请求头
 */
public static void logFormatJson(String msg, String headString) {
    if(mDebuggable < LEVEL_F) {
        return;
    }
    String message;
    try {
        if (msg.startsWith("{")) {
            JSONObject jsonObject = new JSONObject(msg);
            message = jsonObject.toString(4);//最重要的方法,就一行,返回格式化的json字符串,其中的数字4是缩进字符数
        } else if (msg.startsWith("[")) {
            JSONArray jsonArray = new JSONArray(msg);
            message = jsonArray.toString(4);
        } else {
            message = msg;
        }
    } catch (JSONException e) {
        message = msg;
    }
    f( "║════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════");
    message = headString + LINE_SEPARATOR + message;
    String[] lines = message.split(LINE_SEPARATOR);
    for (String line : lines) {
        f( "║ " + line);
    }
    f( "║════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════");
  }

 }
上一篇下一篇

猜你喜欢

热点阅读