How to display long messages in logcat

I am trying to show a long message on logcat. If the message is longer than 1000 characters, it is broken.

What is the mechanism for displaying all characters of a long message in logcat?

+74
android logcat
Sep 30 '11 at 4:50
source share
9 answers

If logcat limits the length to 1000, you can split the string you want to write with String.subString () and register it in chunks. For example:

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)); } 
+128
Sep 30 2018-11-11T00:
source share

As a follow-up, I wrote a wrapper class that handles this for you. You just need to change the import and it will log everything

 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)); } } } 
+27
Oct 08 '14 at 9:33
source share

This is based on a spatula answer, a little more concise and does not add an empty log message at the end:

 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))); } 
+15
Feb 06 '16 at 16:32
source share

Try this piece of code to show a long message in 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 } } 
+6
Apr 16 '15 at 8:29
source share

Here's how OkHttp does it with the HttpLoggingInterceptor:

 public void log(String message) { // Split by line, then ensure each line can fit into Log 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.

Here it uses Log.d (debug) and the hard-coded tag "OkHttp".

It splits the log into new lines or reaches its maximum length.

This class below is a helper class that you can use (if you have lambda support, drop Jack and Jill or retrolambda) to do the same thing that OkHttp does in any log:

 /** * 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 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); } } 
+5
Sep 23 '16 at 8:55
source share

In order not to minimize the separation lines of the log messages, I take a large line and write each line separately.

 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)); } } 
+2
Jun 17 '16 at 2:00
source share

Here is the Kotlin version for @spatulamania's answer (especially for lazy / smart people):

 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)) } 
+1
Oct 03 '17 at 13:55 on
source share

I find Timber a good option for this. Wood automatically splits and prints pieces of message in logcat.

https://github.com/JakeWharton/timber

You can see the implementation of the log method in the static timber.log.Timber.DebugTree class.

0
Jan 13 '18 at 0:49
source share

if you print json string, you can use the code below

  @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) } } } 

full code

CXLogUtil.j ("JSON Tags", "{}")

preview result

0
May 08 '19 at 6:16
source share



All Articles