Android Log日志 - 打印不全问题

AndroidStudio在打印Log的时候目前支持4*1024长度,超出部分不能打印。当你在各种百度之后有对应的解决办法,但是每次都是部分代码,看着都忧伤。索性此次项目调试的数据也是比较多滴,目前就准备对Log开刀来写一个Log类,还是如以往的性格直接写完整的类,方便需要的人用。反正又不是什么高深的东西,为了给被方便同时也是给自己方便。

/**
 * Relin
 * 2019-07-10 10:40:32
 * 日志打印类
 * 该类主要适应日志打印不全问题,主要是因为Log最大长度4*1024,考虑到汉字问题
 * 此处我们采用的是2*1024长度打印,主要是分行打印数据,同时对每行进行[index]标识
 * 目前支持Log.i() Log.w()  Log.e()  Log.d()
 */
public class Log {

    /**
     * 最大一次打印长度
     */
    public final static int MAX_LENGTH = 2000;

    /**
     * 日志类型
     */
    public enum Type {
        I, E, D, W
    }

    /**
     * 打印信息日志
     *
     * @param tag 标志
     * @param msg 内容
     */
    public static void i(String tag, String msg) {
        maxPrint(Type.I, tag, msg);
    }

    /**
     * 打印错误日志
     *
     * @param tag 标志
     * @param msg 内容
     */
    public static void e(String tag, String msg) {
        maxPrint(Type.E, tag, msg);
    }

    /**
     * 打印调试日志
     *
     * @param tag 标志
     * @param msg 内容
     */
    public static void d(String tag, String msg) {
        maxPrint(Type.D, tag, msg);
    }

    /**
     * 打印警告日志
     *
     * @param tag 标志
     * @param msg 打印内容
     */
    public static void w(String tag, String msg) {
        maxPrint(Type.W, tag, msg);
    }


    /**
     * 适应最大长度打印
     *
     * @param type 日志类型
     * @param tag  标志
     * @param msg  信息
     */
    private static void maxPrint(Type type, String tag, String msg) {
        if (msg.length() > MAX_LENGTH) {
            int length = MAX_LENGTH + 1;
            String remain = msg;
            int index = 0;
            while (length > MAX_LENGTH) {
                index++;
                typePrint(type, tag + "[" + index + "]", " \n" + remain.substring(0, MAX_LENGTH));
                remain = remain.substring(MAX_LENGTH);
                length = remain.length();
            }
            if (length <= MAX_LENGTH) {
                index++;
                typePrint(type, tag + "[" + index + "]", " \n" + remain);
            }
        } else {
            typePrint(type, tag, msg);
        }
    }

    /**
     * 打印各种类型
     *
     * @param type 日志类型
     * @param tag  标志
     * @param msg  信息
     */
    private static void typePrint(Type type, String tag, String msg) {
        switch (type) {
            case I:
                android.util.Log.i(tag, msg);
                break;
            case E:
                android.util.Log.e(tag, msg);
                break;
            case W:
                android.util.Log.w(tag, msg);
                break;
            case D:
                android.util.Log.d(tag, msg);
                break;
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值