如何在logcat中显示长消息
我正试图在logcat上显示长消息。 如果消息长度超过1000个字符,则会被破坏。
在logcat中显示长信息的所有字符的机制是什么?
如果logcat的长度是1000,那么你可以用String.subString()分割你想要logging的string,并把它logging下来。 例如:
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)); }
作为spatulamania答案后续我写了一个包装类,为您处理这个。 你只需要改变导入,它会logging一切
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)); } } }
试试这段代码在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 } }
这build立在spatulamania的答案,是一个更简洁一点,不会在最后添加一个空的日志消息:
final int chunkSize = 2048; for (int i = 0; i < s.length(); i += chunkSize) { Log.d(TAG, s.substring(i, Math.min(s.length(), i + chunkSize))); }
为了不使跨越日志消息的分割线最小化,我采取大string并分别logging每行。
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)); } }
这是如何使用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(debug)和硬编码的“OkHttp”标签。
它在新行处或日志达到最大长度时分割日志。
下面这个类是一个帮助类,你可以使用(如果你有lambda支持抛出杰克和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); } }
这里是@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)) }
- Android SDK中的getWidth / Height()和getMeasuredWidth / Height()之间的区别是什么?
- Android:如何获得自定义视图的高度和宽度?
- Android 4.4(KitKat)上的Android图库为Intent.ACTION_GET_CONTENT返回不同的URI
- ViewPager和片段 – 存储片段状态的正确方法是什么?
- 更改ActionBar上溢出button的颜色
- 如何制作一个Android视图,在滑动/滑动视图之间翻转
- 从iOS / Android上的web-app调用本机dateselect器
- Android:检查手机是否为双卡
- Android SQLite数据库什么时候closures