LogUtil日志打印工具

项目开发过程中需要经常打印日志,而上线后的APP我们希望不打印出调试日志,所以自己封装一个日志工具类,可以buildtype来决定是否打印日志:

build.gradle中在debug中定义:

  buildConfigField "int", "ENABLE_DEBUG_LEVEL", "5"

在release中定义:

  buildConfigField "int", "ENABLE_DEBUG_LEVEL", "0"

工具类:

public class LogUtil {

    private LogUtil() {
        throw new UnsupportedOperationException("L cannot instantiated!");
    }

    public static void v(String tag, String msg) {
        if (BuildConfig.ENABLE_DEBUG_LEVEL >= 5)
            Log.v(tag, msg);
    }

    public static void d(String tag, String msg) {
        if (BuildConfig.ENABLE_DEBUG_LEVEL >= 4) {
            if (msg.length() > 3000) {
                for (int i = 0; i < msg.length(); i += 3000) {
                    if (i + 3000 < msg.length())
                        Log.d(tag, msg.substring(i, i + 3000));
                    else
                        Log.d(tag, msg.substring(i, msg.length()));
                }
            } else {
                Log.d(tag, msg);
            }

        }

    }

    public static void i(String tag, String msg) {
        if (BuildConfig.ENABLE_DEBUG_LEVEL >= 3)
            Log.i(tag, msg);
    }

    public static void w(String tag, String msg) {
        if (BuildConfig.ENABLE_DEBUG_LEVEL >= 2)
            Log.w(tag, msg);
    }

    public static void e(String tag, String msg) {
        if (BuildConfig.ENABLE_DEBUG_LEVEL >= 1) {
            if (msg.length() > 3000) {
                for (int i = 0; i < msg.length(); i += 3000) {
                    if (i + 3000 < msg.length())
                        Log.e(tag, msg.substring(i, i + 3000));
                    else
                        Log.e(tag, msg.substring(i, msg.length()));
                }
            } else {
                Log.e(tag, msg);
            }

        }
    }

}

猜你喜欢

转载自blog.csdn.net/u012954039/article/details/81776558
今日推荐