【Java-代碼】
import android.util.Log;
public class LogUtils {
/**
* 手動(dòng)關(guān)閉日志阁将,false關(guān)閉琳状,true打開(kāi)
*/
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) {
// 循環(huán)分段打印日志
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 {
/**
* 手動(dòng)關(guān)閉日志勾笆,false關(guān)閉阿宅,true打開(kāi)
*/
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) {
// 循環(huán)分段打印日志
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)
}
}
}
【測(cè)試】
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() {
//中文字符和英文數(shù)字字符顯示的長(zhǎng)度有所不同
//經(jīng)過(guò)代碼測(cè)試,中文日志不分段顯示的話(huà)洒放,大約能顯示1355個(gè)字符
//數(shù)字英文字符大約能顯示4055個(gè)字符
//所以當(dāng)需要顯示一段比較長(zhǎng)的日志時(shí)蛉鹿,每次顯示1024個(gè)
String msg = "自己復(fù)制粘貼一條很長(zhǎng)的字符顯示,最好超過(guò)4096個(gè)字符";
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) {
//不分割時(shí)榨为,所顯示的數(shù)字字符長(zhǎng)度
LogUtils.v("test-V", "顯示的長(zhǎng)度=" + (s.length() - 1));
}
}
LogUtils.v("test-V", "數(shù)字字符長(zhǎng)度 size=" + s.length());
//不分割,只能顯示字符:4059
LogUtils.v("test-V", s);
LogUtils.v("test-V", "中文字符長(zhǎng)度 size=" + text.length());
//不分割煌茴,只能顯示字符:1353
LogUtils.v("test-V", text);
}
@Test
public void testKt(){
String msg = "自己復(fù)制粘貼一條很長(zhǎng)的字符顯示随闺,最好超過(guò)4096個(gè)字符";
LogUtilsKt.INSTANCE.e("testKt", msg);
}
}