陈力
2023-06-09 cca5f79b3af36e5a908c5dfecbd30110febe3baa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.lotaai.canguiayw.common;
 
import androidx.annotation.NonNull;
 
import com.blankj.utilcode.util.LogUtils;
import com.lotaai.canguiayw.BuildConfig;
 
import java.io.IOException;
 
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
 
public class HttpLoggerInterceptor implements Interceptor {
 
    private static String TAG = "LoggerInterceptor";
    private boolean isDebug;
 
    public HttpLoggerInterceptor(boolean isDebug) {
        this(TAG, isDebug);
    }
 
    public HttpLoggerInterceptor(String tag, boolean isDebug) {
        this.isDebug = isDebug;
        TAG = tag;
    }
 
    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {
        Request request = chain.request();
        if (BuildConfig.DEBUG || isDebug) {
            LogUtils.i(TAG, String.format("发送请求:%s on %s%n%s%n%s",
                    request.url(), chain.connection(), request.headers(), request.body()));
        }
        return chain.proceed(request);
    }
}