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:
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; }