該攔截器用於記錄應用中的網絡請求的信息。
示例
OkHttpClient client = new OkHttpClient(); HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(Level.BASIC); client.interceptors().add(logging); /* 可以通過 setLevel 改變日志級別 共包含四個級別:NONE、BASIC、HEADER、BODY NONE 不記錄 BASIC 請求/響應行 --> POST /greeting HTTP/1.1 (3-byte body) <-- HTTP/1.1 200 OK (22ms, 6-byte body) HEADER 請求/響應行 + 頭 --> Host: example.com Content-Type: plain/text Content-Length: 3 <-- HTTP/1.1 200 OK (22ms) Content-Type: plain/text Content-Length: 6 BODY 請求/響應行 + 頭 + 體 */ // 可以通過實現 Logger 接口更改日志保存位置 HttpLoggingIntercetptor logging = new HttpLoggingInterceptor(new Logger() { @Override public void log(String message) { Timber.tag("okhttp").d(message); } });