log打印添加重定向

This commit is contained in:
2025-12-02 10:11:44 +08:00
parent 6891805e36
commit 6e269d89ec
2 changed files with 31 additions and 7 deletions

View File

@@ -48,7 +48,8 @@ public class DataLoggingInterceptor implements Interceptor {
private final Logger logger;
@NotNull
@Override public Response intercept(@NonNull Chain chain) throws IOException {
@Override
public Response intercept(@NonNull Chain chain) throws IOException {
Request request = chain.request();
logger.reset();
@@ -135,8 +136,11 @@ public class DataLoggingInterceptor implements Interceptor {
if (responseBody != null) {
long contentLength = responseBody.contentLength();
String bodySize = contentLength != -1 ? contentLength + "-byte" : "unknown-length";
// logger.log(response.code() + " " + response.message() + " "
// + response.request().url() + " (" + tookMs + "ms)");
logger.log(response.code() + " " + response.message() + " "
+ response.request().url() + " (" + tookMs + "ms)");
+ response.request().url() + "" + response.networkResponse().request().url() + " (" + tookMs + "ms)");
Headers headers = response.headers();
for (int i = 0, count = headers.size(); i < count; i++) {

View File

@@ -4,6 +4,8 @@ import android.util.Log;
import androidx.annotation.NonNull;
import com.blankj.utilcode.util.LogUtils;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
@@ -34,7 +36,7 @@ public class LogInterceptor implements Interceptor {
private boolean logResponseBody = true;
// 最大日志长度(避免过大的响应体导致日志刷屏)
private int maxLogLength = 2048;
private int maxLogLength = 2048 * 2;
public LogInterceptor() {
}
@@ -136,9 +138,18 @@ public class LogInterceptor implements Interceptor {
}
log.append("==================== 请求结束 ====================\n");
Log.d(TAG, log.toString());
// LogUtils.d(TAG, log.toString());
if (log.toString().contains("Xintiao/keep_xintiao"))
return;
try {
LogUtils.d(log.toString());
} catch (StringIndexOutOfBoundsException e) {
// 处理异常,记录日志或使用默认值
LogUtils.e("打印 JSON 数据时出错:" + e.getMessage());
}
} catch (Exception e) {
Log.e(TAG, "打印请求日志失败: " + e.getMessage());
LogUtils.e(TAG, "打印请求日志失败: " + e.getMessage());
}
}
@@ -186,9 +197,18 @@ public class LogInterceptor implements Interceptor {
}
log.append("==================== 响应结束 ====================\n");
Log.d(TAG, log.toString());
// LogUtils.d(TAG, log.toString());
if (log.toString().contains("Xintiao/keep_xintiao"))
return;
try {
LogUtils.d(log.toString());
} catch (StringIndexOutOfBoundsException e) {
// 处理异常,记录日志或使用默认值
LogUtils.e("打印 JSON 数据时出错:" + e.getMessage());
}
} catch (Exception e) {
Log.e(TAG, "打印响应日志失败: " + e.getMessage());
LogUtils.e(TAG, "打印响应日志失败: " + e.getMessage());
}
}