Android Log打印的统一管理,兼容AndroidStudio的打印过长字符串导致显示不全的问题

用了很久的一个自己封装的Log工具类,因为使用过程中遇到打印JSON时,显示不全,数据太长了,一行显示不完,这边就需要自己判断换行了,比较麻烦,AndroidStudio可以直接以格式化JSON来显示Log,但里面有空格,只是能看看而已,不是很方便,以下是源码

/**
 * @Function Log统一管理类
 * @Auther Lion
 * @Date 16/12/15
 */
public class LogUtil {

    public static boolean isDebug = false; //是否需要打印bug,可以在application的onCreate函数里面初始化
    private static String TAG = "LionYan";
    private static final int STRING_MAXLENGTH = 1000; //Log单行的最大长度
    private static int STRING_START = 0;
    private static int STRING_END = 1000;

    private static final int Level_Verbose = 1;
    private static final int Level_Info = 2;
    private static final int Level_Debug = 3;
    private static final int Level_Warn = 4;
    private static final int Level_Error = 5;

    public static void setDebug(boolean bool) {
        isDebug = bool;
    }


    // 下面四个是默认tag的函数
    public static void i(String msg) {
        handleMessage(Level_Info, TAG, msg);
    }

    public static void d(String msg) {

        handleMessage(Level_Debug, TAG, msg);
    }

    public static void e(String msg) {

        handleMessage(Level_Error, TAG, msg);
    }

    public static void v(String msg) {

        handleMessage(Level_Verbose, TAG, msg);
    }

    public static void w(String msg) {

        handleMessage(Level_Warn, TAG, msg);
    }

    // 下面是传入自定义tag的函数
    public static void i(String tag, String msg) {

        handleMessage(Level_Info, tag, msg);
    }

    public static void d(String tag, String msg) {

        handleMessage(Level_Debug, tag, msg);
    }

    public static void e(String tag, String msg) {

        handleMessage(Level_Error, tag, msg);
    }

    public static void v(String tag, String msg) {

        handleMessage(Level_Verbose, tag, msg);
    }

    public static void w(String tag, String msg) {

        handleMessage(Level_Warn, tag, msg);
    }

    public static void handleMessage(int level, String tag, String message) {
        TAG = tag;
        if (isDebug) {
            STRING_START = 0;
            STRING_END = 1000;
            int msg_difference = message.length() - STRING_MAXLENGTH;
            if (msg_difference > 0) {
                STRING_END = STRING_MAXLENGTH;
                for (; ; ) {
                    showLog(level, tag, message.substring(STRING_START, STRING_END));
                    STRING_START = STRING_END;
                    STRING_END += STRING_MAXLENGTH;
                    if (STRING_END >= message.length()) {
                        showLog(level, TAG, message.substring(STRING_START, message.length()));
                        return;
                    }
                }
            } else {
                showLog(level, tag, message);
            }

        }
    }

    private static void showLog(int level, String tag, String message) {
        switch (level) {
            case Level_Verbose:
                Log.v(tag, message);
                break;
            case Level_Debug:
                Log.d(tag, message);
                break;
            case Level_Info:
                Log.i(tag, message);
                break;
            case Level_Warn:
                Log.w(tag, message);
                break;
            case Level_Error:
                Log.e(tag, message);
                break;
            default:
                break;

        }

    }
}

之后还有一个自己封装的ToastUtils,可传context,不穿的话调用自定义Application的全局context

/**
 * @Function Toast工具类
 * @Auther Lion
 * @Date 16/12/15
 */
public class ToastUtil {

    public static boolean isShow = true;

    public static Toast mToast;

    public static void showToast(Context context, CharSequence message, int showDate) {
        if (isShow) {
            if (mToast != null) {
                cancel();
            }
            Toast toast = Toast.makeText(context, message, showDate);
            toast.show();
            mToast = toast;
        }
    }

    public static void showShort(Context context, CharSequence message) {
        if (isShow) {
            if (mToast != null) {
                cancel();
            }
            showToast(context, message, Toast.LENGTH_SHORT);
        }

    }

    public static void showShort(CharSequence message) {
        if (isShow) {
            if (mToast != null) {
                cancel();
            }
            showToast(MainApplication.getGlobalContext(), message, Toast.LENGTH_SHORT);
        }


    }

    public static void showLong(Context context, CharSequence message) {

        if (isShow) {
            if (mToast != null) {
                cancel();
            }
            showToast(context, message, Toast.LENGTH_LONG);
        }
    }

    public static void showLong(CharSequence message) {

        if (isShow) {
            if (mToast != null) {
                mToast.cancel();
            }
            showToast(MainApplication.getGlobalContext(), message, Toast.LENGTH_LONG);
        }
    }


    public static void cancel() {
        if (mToast != null) {
            mToast.cancel();
        }
    }
}

最后,我将这两个工具类上传到了CSDN,需要的可以去下载,当然~直接在这篇博客里Copy过去也成~

https://download.csdn.net/download/wx_anonymity/10486015

猜你喜欢

转载自blog.csdn.net/wx_anonymity/article/details/80733158