Android常用工具類之 Log工具類

Log工具類推薦一個開源的工具:
https://github.com/MustafaFerhan/DebugLog
源碼只有一個類DebugLog.java
打印出來的內容也很簡潔,但是很方便:
使用方法:

DebugLog.e("I am an error log");

打印效果:

4959-4959/com.example.pc.myapplication E/MainActivity.java﹕ [onClick:45]I am an error log

可以看出:
1. 自動打印Tag,不需要自己定義Tag,會打印出該log所在的類名
2. 自動獲取打印的函數。本例中該log在onClick函數重打印
3. 自動獲取打印log的位置的代碼行數,便於快速定位。

其他好處:
源碼內通過

public static boolean isDebuggable() {
        return BuildConfig.DEBUG;
    }

來確定是否是debug模式,在 build variant 是 ‘release’的時候,會自動隱藏log

下面貼出源碼,感興趣的可以去github中查看更多詳細內容。


/***
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

For more information, please refer to <http://unlicense.org/>
*/

package com.example.pc.myapplication;

import android.util.Log;


/**
 * @date 21.06.2012
 * @author Mustafa Ferhan Akman
 * 
 * Create a simple and more understandable Android logs. 
 * */

public class DebugLog{

    static String className;
    static String methodName;
    static int lineNumber;

    private DebugLog(){
        /* Protect from instantiations */
    }

    public static boolean isDebuggable() {
        return BuildConfig.DEBUG;
    }

    private static String createLog( String log ) {

        StringBuffer buffer = new StringBuffer();
        buffer.append("[");
        buffer.append(methodName);
        buffer.append(":");
        buffer.append(lineNumber);
        buffer.append("]");
        buffer.append(log);

        return buffer.toString();
    }

    private static void getMethodNames(StackTraceElement[] sElements){
        className = sElements[1].getFileName();
        methodName = sElements[1].getMethodName();
        lineNumber = sElements[1].getLineNumber();
    }

    public static void e(String message){
        if (!isDebuggable())
            return;

        // Throwable instance must be created before any methods  
        getMethodNames(new Throwable().getStackTrace());
        Log.e(className, createLog(message));
    }

    public static void i(String message){
        if (!isDebuggable())
            return;

        getMethodNames(new Throwable().getStackTrace());
        Log.i(className, createLog(message));
    }

    public static void d(String message){
        if (!isDebuggable())
            return;

        getMethodNames(new Throwable().getStackTrace());
        Log.d(className, createLog(message));
    }

    public static void v(String message){
        if (!isDebuggable())
            return;

        getMethodNames(new Throwable().getStackTrace());
        Log.v(className, createLog(message));
    }

    public static void w(String message){
        if (!isDebuggable())
            return;

        getMethodNames(new Throwable().getStackTrace());
        Log.w(className, createLog(message));
    }

    public static void wtf(String message){
        if (!isDebuggable())
            return;

        getMethodNames(new Throwable().getStackTrace());
        Log.wtf(className, createLog(message));
    }

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