base的日志打印优化
This commit is contained in:
parent
b2b5db0cd2
commit
be805f4bfd
|
|
@ -17,6 +17,11 @@ public class BaseConstants {
|
|||
* 自定义字符串 打印
|
||||
*/
|
||||
public final static String LOG_PRINT = "LOG_PRINT";
|
||||
|
||||
/**
|
||||
* 自定义字符串 打印线程信息
|
||||
*/
|
||||
public final static String LOG_SHOW_THREADINFI = "LOG_SHOW_THREADINFI";
|
||||
/**
|
||||
* 自定义字符串 隐私权限管理
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -81,6 +81,8 @@ public class LibraryApp {
|
|||
}
|
||||
boolean print = getInstance().isSlotConfig(BaseConstants.LOG_PRINT);
|
||||
LogK.setDebug(print);
|
||||
boolean showThreadInfo = getInstance().isSlotConfig(BaseConstants.LOG_SHOW_THREADINFI);
|
||||
LogK.setShowThreadInfo(showThreadInfo);
|
||||
|
||||
// 初始化 Toast 框架
|
||||
Toaster.init((Application) context);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,31 @@ import com.tfq.library.app.BaseConstants;
|
|||
|
||||
public class LogK {
|
||||
private static final String TAG_PREFIX = "LibraryLog_http";
|
||||
private static boolean DEBUG = false;
|
||||
private static boolean DEBUG = true;
|
||||
// 单条日志最大长度(留出余量给 TAG 等信息)
|
||||
private static final int MAX_LOG_LENGTH = 3500;
|
||||
// 是否显示线程信息(默认不显示)
|
||||
private static boolean showThreadInfo = false;
|
||||
|
||||
/**
|
||||
* 初始化日志
|
||||
*
|
||||
* @param debug 是否开启调试模式
|
||||
*/
|
||||
public static void init(boolean debug) {
|
||||
DEBUG = debug;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化日志
|
||||
*
|
||||
* @param debug 是否开启调试模式
|
||||
* @param showThreadInfo 是否显示线程信息
|
||||
*/
|
||||
public static void init(boolean debug, boolean showThreadInfo) {
|
||||
DEBUG = debug;
|
||||
LogK.showThreadInfo = showThreadInfo;
|
||||
}
|
||||
|
||||
public static boolean isDebug() {
|
||||
return DEBUG;
|
||||
|
|
@ -16,27 +40,112 @@ public class LogK {
|
|||
DEBUG = debug;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否显示线程信息
|
||||
*
|
||||
* @param showThreadInfo 是否显示线程信息
|
||||
*/
|
||||
public static void setShowThreadInfo(boolean showThreadInfo) {
|
||||
LogK.showThreadInfo = showThreadInfo;
|
||||
}
|
||||
|
||||
public static void d(String msg) {
|
||||
if (DEBUG) Log.d(TAG_PREFIX, buildMessage(msg));
|
||||
if (DEBUG) printLog(Log.DEBUG, msg);
|
||||
}
|
||||
|
||||
public static void e(String msg) {
|
||||
if (DEBUG) Log.e(TAG_PREFIX, buildMessage(msg));
|
||||
if (DEBUG) printLog(Log.ERROR, msg);
|
||||
}
|
||||
|
||||
public static void e2(String msg) {
|
||||
if (DEBUG) printLog(Log.ERROR, msg);
|
||||
}
|
||||
|
||||
public static void i(String msg) {
|
||||
if (DEBUG) Log.i(TAG_PREFIX, buildMessage(msg));
|
||||
if (DEBUG) printLog(Log.INFO, msg);
|
||||
}
|
||||
|
||||
public static void w(String msg) {
|
||||
if (DEBUG) Log.w(TAG_PREFIX, buildMessage(msg));
|
||||
if (DEBUG) printLog(Log.WARN, msg);
|
||||
}
|
||||
|
||||
public static void v(String msg) {
|
||||
if (DEBUG) Log.v(TAG_PREFIX, buildMessage(msg));
|
||||
if (DEBUG) printLog(Log.VERBOSE, msg);
|
||||
}
|
||||
|
||||
private static String buildMessage(String msg) {
|
||||
/**
|
||||
* 打印日志(自动分段处理长日志)
|
||||
*
|
||||
* @param priority 日志优先级
|
||||
* @param msg 日志消息
|
||||
*/
|
||||
private static void printLog(int priority, String msg) {
|
||||
if (msg == null) {
|
||||
msg = "null";
|
||||
}
|
||||
|
||||
String prefix;
|
||||
if (showThreadInfo) {
|
||||
// 获取调用者信息
|
||||
String caller = getCallerInfo();
|
||||
prefix = "[" + Thread.currentThread().getId() + "] " + caller + " ▶ ";
|
||||
} else {
|
||||
// 不显示线程信息,只显示简单前缀
|
||||
prefix = "";
|
||||
}
|
||||
|
||||
int length = msg.length();
|
||||
|
||||
// 如果消息不长,直接打印
|
||||
if (length <= MAX_LOG_LENGTH) {
|
||||
Log.println(priority, TAG_PREFIX, prefix + msg);
|
||||
return;
|
||||
}
|
||||
|
||||
// 长消息分段打印
|
||||
int index = 0;
|
||||
int part = 1;
|
||||
int totalParts = (length + MAX_LOG_LENGTH - 1) / MAX_LOG_LENGTH;
|
||||
|
||||
while (index < length) {
|
||||
int end = Math.min(index + MAX_LOG_LENGTH, length);
|
||||
String chunk = msg.substring(index, end);
|
||||
|
||||
if (totalParts > 1) {
|
||||
boolean isFirstPart = (part == 1);
|
||||
boolean isLastPart = (part == totalParts);
|
||||
|
||||
// 分段打印时不显示 TAG 前缀
|
||||
if (isFirstPart) {
|
||||
// 第一段:前面打印一个空行
|
||||
Log.println(priority, TAG_PREFIX, "");
|
||||
Log.println(priority, TAG_PREFIX, "━━━━━━━ START SEGMENT LOG ━━━━━━━");
|
||||
Log.println(priority, TAG_PREFIX, "━━━━━━━━━━━ [" + part + "/" + totalParts + "] ━━━━━━━━━━━");
|
||||
Log.println(priority, TAG_PREFIX, chunk);
|
||||
} else if (isLastPart) {
|
||||
// 最后段:打印分段标识和内容,后面打印一个空行
|
||||
Log.println(priority, TAG_PREFIX, "━━━━━━━━━━━ [" + part + "/" + totalParts + "] ━━━━━━━━━━━");
|
||||
Log.println(priority, TAG_PREFIX, chunk);
|
||||
Log.println(priority, TAG_PREFIX, "━━━━━━ SEGMENT LOG COMPLETE ━━━━━");
|
||||
Log.println(priority, TAG_PREFIX, "");
|
||||
} else {
|
||||
// 中间的段:只打印分段标识和内容
|
||||
Log.println(priority, TAG_PREFIX, "━━━━━━━━━━━ [" + part + "/" + totalParts + "] ━━━━━━━━━━━");
|
||||
Log.println(priority, TAG_PREFIX, chunk);
|
||||
}
|
||||
} else {
|
||||
Log.println(priority, TAG_PREFIX, prefix + chunk);
|
||||
}
|
||||
|
||||
index = end;
|
||||
part++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取调用者信息(类名.方法名:行号)
|
||||
*/
|
||||
private static String getCallerInfo() {
|
||||
StackTraceElement[] trace = new Throwable().getStackTrace();
|
||||
String caller = "<unknown>";
|
||||
|
||||
|
|
@ -49,6 +158,6 @@ public class LogK {
|
|||
break;
|
||||
}
|
||||
}
|
||||
return "[" + Thread.currentThread().getId() + "] " + caller + " ▶ " + msg;
|
||||
return caller;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
|
@ -1 +1 @@
|
|||
ad674ab399963d77dbe3b30252397161
|
||||
62fc1f0de1c7693fca49dd1c648777aa
|
||||
|
|
@ -1 +1 @@
|
|||
02ba57a737b968b4ba275c994b34e3aaaf87e58a
|
||||
9c823259cc1f5f247d92bdde361793bdecc55e4f
|
||||
|
|
@ -1 +1 @@
|
|||
24a19294eabb61a9ea38707038b0adc6ca6a84c397d88b1d90803109ff43d0f0
|
||||
6fa9248d729972ea6c92a75ac2fe0f30439e4bc3e2c43a66137f0c578bcccc14
|
||||
|
|
@ -1 +1 @@
|
|||
daeb64e99aafa0c63bf4a2e8e01ef5855263e168c20c7f67835113962b592096f890486ce241a88939d8444c82ffccc6021af51453323ec2d54d8c279e4be9fa
|
||||
5e2d31bd114d3cbad4873eae250469e9d308e793d5106d5aadef2555852e235f66da00be897cf5fb3a32a4aeaee93ce8f489239f18aec7dd1fe25f67b98d95ff
|
||||
|
|
@ -10,6 +10,6 @@
|
|||
<version>1.0.1</version>
|
||||
<version>1.1.0</version>
|
||||
</versions>
|
||||
<lastUpdated>20260112010551</lastUpdated>
|
||||
<lastUpdated>20260119063629</lastUpdated>
|
||||
</versioning>
|
||||
</metadata>
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
890b378841c1dfa446e675ab4959e931
|
||||
73d52ed63fbc7e9e768bea4689cfe83c
|
||||
|
|
@ -1 +1 @@
|
|||
a680c4873b7d3b25fb4e1fcd2c3ea8ace4cdcc7c
|
||||
320fe22619ad19a5d822ba7fb7bfc09d45cee0b4
|
||||
|
|
@ -1 +1 @@
|
|||
33343dcda6d86941c9a6c7345cca1a0c8891be557f59548c799eec028d56f6ff
|
||||
6e8927fa0be26b71cb3f234a1f311250224b691f3639e56114b6e34898a71471
|
||||
|
|
@ -1 +1 @@
|
|||
ded7f88c2eae7902a703eca8a275bc0cc9bf4c8811df1b56bfa4c2e86efb2c4e95bcb26e47e2de54b69437dd6432c056970f10f38b2c0396c561decd170b388c
|
||||
95048eaa09b3fc69cf2116193fe784d017f697501c096eac104b13a746fbdb5a394999568ae3f0a8b8ff555162928d4424c74a9d49b4eecbcaf91c85b4f86edc
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
rootProject.name = "test"
|
||||
rootProject.name = "同风起Lib"
|
||||
include ':BaseLibrary'
|
||||
include ':LibraryAd'
|
||||
include ':LibraryAdLib'
|
||||
|
|
|
|||
Loading…
Reference in New Issue