I am trying to display long message on logcat. If the length of message is more than 1000 characters, it gets broken.
What is the mechanism to show all characters o
if print json string, can use 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-tag","{}")
This is how OkHttp with HttpLoggingInterceptor does it:
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
is 4000.
Here it use Log.d (debug) and hardcoded "OkHttp" tag.
It split the log at newlines or when it reach the max length.
This class below is an helper class you can use (if you have lambda support throw Jack & Jill or retrolambda) to do the same thing OkHttp does on 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'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);
}
}
Try this piece of code to show 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
}
}
As a follow on to spatulamania answer I wrote a wrapper class which 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));
}
}
}
This builds on spatulamania's answer, is a little more succinct, and won't 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)));
}
I consider Timber a good option for this issue. Timber automatically split and print chunks of message in logcat.
https://github.com/JakeWharton/timber
You can see log method implementation in timber.log.Timber.DebugTree static class.