Logger-功能強大使用方便的Log日誌打印工具類

介紹

Android開發中Log日誌打印對開發者來說是非常重要的功能。但是感覺Log寫多了也是煩,每次都需要設置一個TAG過濾值和具體的打印信息,而且打印的信息也不夠豐富。
這裏我推薦一個功能強大的Log日誌打印工具類-Logger。(不是java.util.logging.Logger,只是恰好同名而已)

使用

使用非常簡單,直接調用靜態類方法。提供Debug/Info/Error三個級別的打印方法。每個方法又分爲兩種,有參數和空參的兩種。

        Logger.d();
        Logger.d("log message");

使用效果

有圖爲證。打印效果,格式說明。
默認TAG: [ 執行類:調用的方法:代碼行數 ] :打印信息
打印效果
打出的信息非常豐富,有方法和代碼行數。方便我們定位代碼。還可以在版本發佈後,統一關閉日誌打印功能。很好的滿足了我們的開發日誌打印功能。

源碼及說明

代碼說明

  • TAG在工具類中設置好,每個App項目都有特定的TAG。
  • 設計3個布爾值,作爲用來控制打印的開關,默認ture。在版本發佈後改爲false就好了。
  • 每個打印,都有兩個重載方法,有參無參,根據實際情況使用。
  • 最後getLocation方法,獲取堆棧信息,提供打印方法和代碼行功能。

源碼

public final class Logger {

    private static final String TAG = "Demo";

    /**
     * Set true or false if you want read logs or not
     */
    private static boolean logEnabled_d = true;
    private static boolean logEnabled_i = true;
    private static boolean logEnabled_e = true;

    public static void d() {
        if (logEnabled_d) {
            android.util.Log.d(TAG, getLocation());
        }
    }

    public static void d(String msg) {
        if (logEnabled_d) {
            android.util.Log.d(TAG, getLocation() + msg);
        }
    }

    public static void i(String msg) {
        if (logEnabled_i) {
            android.util.Log.i(TAG, getLocation() + msg);
        }
    }

    public static void i() {
        if (logEnabled_i) {
            android.util.Log.i(TAG, getLocation());
        }
    }

    public static void e(String msg) {
        if (logEnabled_e) {
            android.util.Log.e(TAG, getLocation() + msg);
        }
    }

    public static void e(String msg, Throwable e) {
        if (logEnabled_e) {
            android.util.Log.e(TAG, getLocation() + msg, e);
        }
    }

    public static void e(Throwable e) {
        if (logEnabled_e) {
            android.util.Log.e(TAG, getLocation(), e);
        }
    }

    public static void e() {
        if (logEnabled_e) {
            android.util.Log.e(TAG, getLocation());
        }
    }

    private static String getLocation() {
        final String className = Logger.class.getName();
        final StackTraceElement[] traces = Thread.currentThread()
                .getStackTrace();

        boolean found = false;

        for (StackTraceElement trace : traces) {
            try {
                if (found) {
                    if (!trace.getClassName().startsWith(className)) {
                        Class<?> clazz = Class.forName(trace.getClassName());
                        return "[" + getClassName(clazz) + ":"
                                + trace.getMethodName() + ":"
                                + trace.getLineNumber() + "]: ";
                    }
                } else if (trace.getClassName().startsWith(className)) {
                    found = true;
                }
            } catch (ClassNotFoundException ignored) {
            }
        }

        return "[]: ";
    }

    private static String getClassName(Class<?> clazz) {
        if (clazz != null) {
            if (!TextUtils.isEmpty(clazz.getSimpleName())) {
                return clazz.getSimpleName();
            }

            return getClassName(clazz.getEnclosingClass());
        }

        return "";
    }

}

來源

其實這也不是我寫的,只是在開發的時候發現了,分享出來。
代碼來源是:Yalantis公司的開源項目中的工具類。
Yalantis公司有很多效果非常酷炫的開源項目,大家可以上github上看看。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章