Android Log 日志工具,显示超长日志

【Java-代码】

import android.util.Log;

public class LogUtils {

    /**
     * 手动关闭日志,false关闭,true打开
     */
    private static boolean mLogAble = isDebug();

    private static boolean isDebug() {
        return true;
    }


    public static void v(String tag, String msg) {
        v(tag, msg, (Throwable) null);
    }

    public static void v(String tag, String msg, Throwable ex) {
        showLongLog(0, tag, msg, ex);
    }

    public static void d(String tag, String msg) {
        d(tag, msg, (Throwable) null);
    }

    public static void d(String tag, String msg, Throwable ex) {
        showLongLog(1, tag, msg, ex);
    }

    public static void i(String tag, String msg) {
        i(tag, msg, (Throwable) null);
    }

    public static void i(String tag, String msg, Throwable ex) {
        showLongLog(2, tag, msg, ex);
    }


    public static void w(String tag, String msg) {
        w(tag, msg, (Throwable) null);
    }

    public static void w(String tag, String msg, Throwable ex) {
        showLongLog(3, tag, msg, ex);
    }


    public static void e(String tag, String msg) {
        e(tag, msg, (Throwable) null);
    }

    public static void e(String tag, String msg, Throwable ex) {
        showLongLog(4, tag, msg, ex);
    }

    private static void showLongLog(int level, String tag, String msg, Throwable tr) {
        if (mLogAble) {
            msg = msg + '\n' + Log.getStackTraceString(tr);
            //为了显示完整中文日志,1024
            int segmentSize = 1024;
            long length = msg.length();

            if (length > segmentSize) {
                while (msg.length() > segmentSize) {
                    // 循环分段打印日志
                    String logContent = msg.substring(0, segmentSize);
                    showLog(level, tag,  logContent);
                    msg = msg.substring(logContent.length());
                }
            }
            showLog(level, tag,  msg);
        }
    }

    private static void showLog(int level, String tag, String msg) {
        switch (level) {
            case 1:
                Log.d(tag, msg);
                break;
            case 2:
                Log.i(tag, msg);
                break;
            case 3:
                Log.w(tag, msg);
                break;
            case 4:
                Log.e(tag, msg);
                break;
            default:
                Log.v(tag, msg);
                break;
        }
    }

}



【Kotlin-代码】

import android.util.Log

object LogUtilsKt {
    /**
     * 手动关闭日志,false关闭,true打开
     */
    private val mLogAble = isDebug

    private val isDebug: Boolean
        private get() = true

    @JvmOverloads
    fun v(tag: String, msg: String, ex: Throwable? = null as Throwable?) {
        showLongLog(0, tag, msg, ex)
    }

    @JvmOverloads
    fun d(tag: String, msg: String, ex: Throwable? = null as Throwable?) {
        showLongLog(1, tag, msg, ex)
    }

    @JvmOverloads
    fun i(tag: String, msg: String, ex: Throwable? = null as Throwable?) {
        showLongLog(2, tag, msg, ex)
    }

    @JvmOverloads
    fun w(tag: String, msg: String, ex: Throwable? = null as Throwable?) {
        showLongLog(3, tag, msg, ex)
    }

    @JvmOverloads
    fun e(tag: String, msg: String, ex: Throwable? = null as Throwable?) {
        showLongLog(4, tag, msg, ex)
    }

    private fun showLongLog(level: Int, tag: String, msg: String, tr: Throwable?) {
        var message = msg
        if (mLogAble) {
            message = """
                $message
                ${Log.getStackTraceString(tr)}
                """.trimIndent()

            //为了显示完整中文日志,1024
            val segmentSize = 1024
            val length = message.length.toLong()

            if (length > segmentSize) {
                while (message.length > segmentSize) {
                    // 循环分段打印日志
                    val logContent = message.substring(0, segmentSize)
                    showLog(level, tag, logContent)
                    message = message.substring(logContent.length)
                }
            }
            showLog(level, tag, message)
        }
    }

    private fun showLog(level: Int, tag: String, msg: String) {
        when (level) {
            1 -> Log.d(tag, msg)
            2 -> Log.i(tag, msg)
            3 -> Log.w(tag, msg)
            4 -> Log.e(tag, msg)
            else -> Log.v(tag, msg)
        }
    }
}



【测试】

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.filters.MediumTest;
import com.technology.myapplication.utils.LogUtils;
import com.technology.myapplication.utils.LogUtilsKt;
import org.junit.Test;
import org.junit.runner.RunWith;

@MediumTest
@RunWith(AndroidJUnit4.class)
@LargeTest
public class LogExampleUnitTest {

    @Test
    public void testV() {
        //中文字符和英文数字字符显示的长度有所不同
        //经过代码测试,中文日志不分段显示的话,大约能显示1355个字符
        //数字英文字符大约能显示4055个字符
        //所以当需要显示一段比较长的日志时,每次显示1024个

        String msg = "自己复制粘贴一条很长的字符显示,最好超过4096个字符";
        LogUtils.v("test-V", "size=" + msg.length());
        //不分割,只能显示字符:1355
        LogUtils.v("test-V", msg);

        String s = "";
        String text = "";
        for (int i = 0; i < 10000; i++) {
            s = s + "." + i;
            text += "我";

            if (i == 1033) {
                //不分割时,所显示的数字字符长度
                LogUtils.v("test-V", "显示的长度=" + (s.length() - 1));
            }
        }

        LogUtils.v("test-V", "数字字符长度 size=" + s.length());
        //不分割,只能显示字符:4059
        LogUtils.v("test-V", s);

        LogUtils.v("test-V", "中文字符长度 size=" + text.length());
        //不分割,只能显示字符:1353
        LogUtils.v("test-V", text);
    }

    @Test
    public void testKt(){
        String msg = "自己复制粘贴一条很长的字符显示,最好超过4096个字符";
        LogUtilsKt.INSTANCE.e("testKt", msg);
    }
}

你可能感兴趣的:(Android Log 日志工具,显示超长日志)