我正在尝试在logcat上显示长消息。如果消息的长度超过1000个字符,它将被打断。
在logcat中显示长消息的所有字符的机制是什么?
我正在尝试在logcat上显示长消息。如果消息的长度超过1000个字符,它将被打断。
在logcat中显示长消息的所有字符的机制是什么?
Answers:
如果logcat将长度限制为1000,则可以使用String.subString()分割要记录的字符串,然后将其分段记录。例如:
int maxLogSize = 1000;
for(int i = 0; i <= veryLongString.length() / maxLogSize; i++) {
int start = i * maxLogSize;
int end = (i+1) * maxLogSize;
end = end > veryLongString.length() ? veryLongString.length() : end;
Log.v(TAG, veryLongString.substring(start, end));
}
veryLongString.length()
是的倍数)maxLogSize
。也许将更<=
改为<
。
作为spatulamania答案的后续部分,我编写了一个包装类,该包装类可以为您处理。您只需要更改导入,它将记录所有内容
public class Log {
public static void d(String TAG, String message) {
int maxLogSize = 2000;
for(int i = 0; i <= message.length() / maxLogSize; i++) {
int start = i * maxLogSize;
int end = (i+1) * maxLogSize;
end = end > message.length() ? message.length() : end;
android.util.Log.d(TAG, message.substring(start, end));
}
}
}
这是带有HttpLoggingInterceptor的OkHttp的执行方式:
public void log(String message) {
// Split by line, then ensure each line can fit into Log's maximum length.
for (int i = 0, length = message.length(); i < length; i++) {
int newline = message.indexOf('\n', i);
newline = newline != -1 ? newline : length;
do {
int end = Math.min(newline, i + MAX_LOG_LENGTH);
Log.d("OkHttp", message.substring(i, end));
i = end;
} while (i < newline);
}
}
MAX_LOG_LENGTH
是4000。
在这里,它使用Log.d(调试)和硬编码的“ OkHttp”标签。
它在换行符或达到最大长度时拆分日志。
下面的此类是您可以使用的帮助程序类(如果您有lambda支持抛出Jack&Jill或retrolambda),则可以在任何日志上执行OkHttp的相同操作:
/**
* Help printing logs splitting text on new line and creating multiple logs for too long texts
*/
public class LogHelper {
private static final int MAX_LOG_LENGTH = 4000;
public static void v(@NonNull String tag, @Nullable String message) {
log(message, line -> Log.v(tag, line));
}
public static void d(@NonNull String tag, @Nullable String message) {
log(message, line -> Log.d(tag, line));
}
public static void i(@NonNull String tag, @Nullable String message) {
log(message, line -> Log.i(tag, line));
}
public static void w(@NonNull String tag, @Nullable String message) {
log(message, line -> Log.w(tag, line));
}
public static void e(@NonNull String tag, @Nullable String message) {
log(message, line -> Log.e(tag, line));
}
public static void v(@NonNull String tag, @Nullable String message, @Nullable Throwable throwable) {
log(message, throwable, line -> Log.v(tag, line));
}
public static void d(@NonNull String tag, @Nullable String message, @Nullable Throwable throwable) {
log(message, throwable, line -> Log.d(tag, line));
}
public static void i(@NonNull String tag, @Nullable String message, @Nullable Throwable throwable) {
log(message, throwable, line -> Log.i(tag, line));
}
public static void w(@NonNull String tag, @Nullable String message, @Nullable Throwable throwable) {
log(message, throwable, line -> Log.w(tag, line));
}
public static void e(@NonNull String tag, @Nullable String message, @Nullable Throwable throwable) {
log(message, throwable, line -> Log.e(tag, line));
}
private static void log(@Nullable String message, @NonNull LogCB callback) {
if (message == null) {
callback.log("null");
return;
}
// Split by line, then ensure each line can fit into Log's maximum length.
for (int i = 0, length = message.length(); i < length; i++) {
int newline = message.indexOf('\n', i);
newline = newline != -1 ? newline : length;
do {
int end = Math.min(newline, i + MAX_LOG_LENGTH);
callback.log(message.substring(i, end));
i = end;
} while (i < newline);
}
}
private static void log(@Nullable String message, @Nullable Throwable throwable, @NonNull LogCB callback) {
if (throwable == null) {
log(message, callback);
return;
}
if (message != null) {
log(message + "\n" + Log.getStackTraceString(throwable), callback);
} else {
log(Log.getStackTraceString(throwable), callback);
}
}
private interface LogCB {
void log(@NonNull String message);
}
}
尝试这段代码以在logcat中显示长消息。
public void logLargeString(String str) {
if(str.length() > 3000) {
Log.i(TAG, str.substring(0, 3000));
logLargeString(str.substring(3000));
} else {
Log.i(TAG, str); // continuation
}
}
为了不使跨日志消息的分割线最小化,我采用大字符串并分别记录每行。
void logMultilineString(String data) {
for (String line : data.split("\n")) {
logLargeString(line);
}
}
void logLargeString(String data) {
final int CHUNK_SIZE = 4076; // Typical max logcat payload.
int offset = 0;
while (offset + CHUNK_SIZE <= data.length()) {
Log.d(TAG, data.substring(offset, offset += CHUNK_SIZE));
}
if (offset < data.length()) {
Log.d(TAG, data.substring(offset));
}
}
这是@spatulamania答案的Kotlin版本(尤其是对于懒惰/聪明的人):
val maxLogSize = 1000
val stringLength = yourString.length
for (i in 0..stringLength / maxLogSize) {
val start = i * maxLogSize
var end = (i + 1) * maxLogSize
end = if (end > yourString.length) yourString.length else end
Log.v("YOURTAG", yourString.substring(start, end))
}
我认为Timber是解决此问题的不错选择。木材自动在logcat中拆分和打印消息块。
https://github.com/JakeWharton/timber
您可以在timber.log.Timber.DebugTree静态类中看到日志方法的实现。
如果打印json字符串,可以使用下面的代码
@JvmStatic
fun j(level: Int, tag: String? = null, msg: String) {
if (debug) {
if (TextUtils.isEmpty(msg)) {
p(level, tag, msg)
} else {
val message: String
message = try {
when {
msg.startsWith("{") -> {
val jsonObject = JSONObject(msg)
jsonObject.toString(4)
}
msg.startsWith("[") -> {
val jsonArray = JSONArray(msg)
jsonArray.toString(4)
}
else -> msg
}
} catch (e: JSONException) {
e.printStackTrace()
msg
}
p(level, tag, "╔═══════════════════════════════════════════════════════════════════════════════════════", false)
val lines = message.split(LINE_SEPARATOR.toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
for (line in lines) {
p(level, tag, "║ $line", false)
}
p(level, tag, "╚═══════════════════════════════════════════════════════════════════════════════════════", false)
}
}
}
CXLogUtil.j(“ json-tag”,“ {}”)