xutils 的 LogUtil

偶然翻到 xutils 源碼中的 LogUtil 工具類,覺得挺不錯,果斷 copy 過來備用。

它巧妙運用了 Throwable ,可以定位到 類名 -> 方法名 -> 行號,對於調試來說,非常方便。
代碼如下:

import android.text.TextUtils;
import android.util.Log;

public class LogUtil {
    //自定 Tag 前綴,可以修改
    public static String customTagPrefix = "x_log";

    //是否是 debug 模式,如果 isDebug = true,不打印log日誌。
    private static boolean isDebug = true;

    /**
     * 通過此方法,設置 isDebug 的值,控制是否打印log日誌。
     * @param isDebug 是否是 debug 模式,如果 isDebug = true,不打印log日誌。
     */
    public static void setDebug(boolean isDebug) {
        LogUtil.isDebug = isDebug;
    }

    public static boolean isDebug() {
        return isDebug;
    }

    private LogUtil() {
    }

    private static String generateTag() {
        StackTraceElement caller = (new Throwable()).getStackTrace()[2];
        String tag = "%s.%s(L:%d)";
        String callerClazzName = caller.getClassName();
        callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
        tag = String.format(tag, callerClazzName, caller.getMethodName(), caller.getLineNumber());
        tag = TextUtils.isEmpty(customTagPrefix) ? tag : customTagPrefix + ":" + tag;
        return tag;
    }

    public static void d(String content) {
        if (isDebug()) {
            String tag = generateTag();
            Log.d(tag, content);
        }
    }

    public static void d(String content, Throwable tr) {
        if (isDebug()) {
            String tag = generateTag();
            Log.d(tag, content, tr);
        }
    }

    public static void e(String content) {
        if (isDebug()) {
            String tag = generateTag();
            Log.e(tag, content);
        }
    }

    public static void e(String content, Throwable tr) {
        if (isDebug()) {
            String tag = generateTag();
            Log.e(tag, content, tr);
        }
    }

    public static void i(String content) {
        if (isDebug()) {
            String tag = generateTag();
            Log.i(tag, content);
        }
    }

    public static void i(String content, Throwable tr) {
        if (isDebug()) {
            String tag = generateTag();
            Log.i(tag, content, tr);
        }
    }

    public static void v(String content) {
        if (isDebug()) {
            String tag = generateTag();
            Log.v(tag, content);
        }
    }

    public static void v(String content, Throwable tr) {
        if (isDebug()) {
            String tag = generateTag();
            Log.v(tag, content, tr);
        }
    }

    public static void w(String content) {
        if (isDebug()) {
            String tag = generateTag();
            Log.w(tag, content);
        }
    }

    public static void w(String content, Throwable tr) {
        if (isDebug()) {
            String tag = generateTag();
            Log.w(tag, content, tr);
        }
    }

    public static void w(Throwable tr) {
        if (isDebug()) {
            String tag = generateTag();
            Log.w(tag, tr);
        }
    }

    public static void wtf(String content) {
        if (isDebug()) {
            String tag = generateTag();
            Log.wtf(tag, content);
        }
    }

    public static void wtf(String content, Throwable tr) {
        if (isDebug()) {
            String tag = generateTag();
            Log.wtf(tag, content, tr);
        }
    }

    public static void wtf(Throwable tr) {
        if (isDebug()) {
            String tag = generateTag();
            Log.wtf(tag, tr);
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章