回答
如果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));
}
登錄貓只打印出響應的一半..我怎麼能得到整個響應的長度。你說的非常LongString。length()但在這裏它只打印了一半響應時,我打印日誌結果在日誌貓 – Vasu
但在iphone控制檯我得到整個響應字符串 – Vasu
你可以通過寫入長度()日誌來檢查響應的長度。如果此值不符合您的預期,則問題可能不在於日誌記錄。 – spatulamania
作爲關於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));
}
}
}
試試這段代碼在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
}
}
此基礎上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)));
}
謝謝。不建議使用超過3000個符號,我這樣使用。 – CoolMind
爲了不降低整個日誌消息分割線,我把大字符串分開記錄每行。
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));
}
}
這是怎麼OkHttp與HttpLoggingInterceptor做的:
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」 標籤。
它在新行處或日誌達到最大長度時拆分日誌。
下面這個類是一個輔助類,你可以使用(如果你有拉姆達支持罰球傑克&吉爾或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 answer(尤其是懶/智能人):
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))
}
我認爲木材是這個問題的一個很好的選擇。木材自動分割並在logcat中打印大塊消息。
https://github.com/JakeWharton/timber
您可以查看日誌timber.log.Timber.DebugTree靜態類方法實現。
- 1. Android不顯示logcat消息
- 2. 如何增加Logcat消息長度?
- 3. Logcat不顯示調試級別消息
- 4. 在AlertDialog中顯示長消息
- 5. 如何logcat消息保存
- 6. adb logcat:增加最大消息長度
- 7. 只有橙色的消息顯示在Android Logcat中
- 8. logcat的沒有顯示在Android Studio中調試消息
- 9. 如何在alt消息中顯示錶
- 10. 如何在SBT中顯示消息?
- 11. 如何在javascript中顯示消息框
- 12. 如何在r中顯示消息?
- 13. 如何在Maven中顯示消息
- 14. 如何在secondActivity中顯示Toast消息?
- 15. eclipse中的Android LogCat:如何過濾LogCat消息?
- 16. Android設備監視器中的Logcat只顯示某些消息
- 17. 未知消息中的logcat
- 18. 如何顯示在從類敬酒消息不延長活動
- 19. Logcat在AVD上顯示信息3次
- 20. 如何在SDL Tridion消息欄中顯示自定義消息?
- 21. 如何在消息框中顯示此消息?
- 22. PHPmailer如何顯示消息
- 23. android - 如何顯示消息
- 24. 如何顯示Toast消息?
- 25. 在ExtJs中顯示消息
- 26. 在c中顯示消息#
- 27. 在logcat中顯示system.out.println
- 28. 如何在POV顯示中顯示不同的消息?
- 29. 如何在C#asp.net窗體中顯示/顯示消息框?
- 30. 爲什麼LogCat不顯示完整的消息?
爲什麼要在logcat中顯示這麼大的字符串? –
我得到服務器的響應作爲一個長字符串。 – Vasu
即使是這樣,爲什麼你要打印的整個字符串,其寫入文件或數據庫,並查看有 - 如果它用於調試 –