투케이2K

707. (Android/Java) okhttp 네트워크 통신 라이브러리 사용해 logging http 통신 전문 로그 출력 - interceptor 본문

Android

707. (Android/Java) okhttp 네트워크 통신 라이브러리 사용해 logging http 통신 전문 로그 출력 - interceptor

투케이2K 2023. 12. 6. 18:40

[개발 환경 설정]

개발 툴 : AndroidStudio

 

[소스 코드]

 

    // -----------------------------------------------------------
    // [build.gradle 파일 의존 설정] : [okhttp 통신 라이브러리]
    // -----------------------------------------------------------
    implementation("com.squareup.okhttp3:okhttp:4.9.0")
    implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'
    // -----------------------------------------------------------




     // ------------------------------------------------------
     // TODO [HTTP 통신 전문 로그 기록]
     // ------------------------------------------------------
     HttpLoggingInterceptor httpLogger = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
         @Override
         public void log(String message) {
             Log.e("HTTP-LOG", "LOGGER : " + message);
         }
     });
     httpLogger.setLevel(HttpLoggingInterceptor.Level.BODY);





     // ------------------------------------------------------
     // TODO [OK HTTP 객체 선언 및 HTTP 통신 로그 출력 옵션 지정] : addInterceptor
     // ------------------------------------------------------
     OkHttpClient client = new OkHttpClient.Builder()
             .connectTimeout(TIME_OUT_SECOND, TimeUnit.SECONDS) // [커넥션 제한 시간]
             .readTimeout(TIME_OUT_SECOND, TimeUnit.SECONDS) // [read 타임 아웃]
             .writeTimeout(TIME_OUT_SECOND, TimeUnit.SECONDS) // [write 타임 아웃]
             .addInterceptor(httpLogger) // [Http 통신 로그]
             .retryOnConnectionFailure(false)
             .build();

[결과 출력]

E/HTTP-LOG: LOGGER : --> GET https://jsonplaceholder.typicode.com/posts?id=1&userId=1
E/HTTP-LOG: LOGGER : Content-Type: application/x-www-form-urlencoded; charset=utf-8;
E/HTTP-LOG: LOGGER : Cache-Control: no-cache
E/HTTP-LOG: LOGGER : --> END GET
E/HTTP-LOG: LOGGER : <-- 200 https://jsonplaceholder.typicode.com/posts?id=1&userId=1 (1196ms)
E/HTTP-LOG: LOGGER : date: Tue, 05 Dec 2023 22:58:10 GMT
E/HTTP-LOG: LOGGER : content-type: application/json; charset=utf-8
E/HTTP-LOG: LOGGER : report-to: {"group":"heroku-nel","max_age":3600,"endpoints":[{"url":"https://nel.heroku.com/reports?ts=1701817089&sid=e11707d5-02a7-43ef-b45e-2cf4d2036f7d&s=J3fL%2F4tA5kpLZHlXYJViq54A88If1JhdAgE6v0fx9Kw%3D"}]}
E/HTTP-LOG: LOGGER : reporting-endpoints: heroku-nel=https://nel.heroku.com/reports?ts=1701817089&sid=e11707d5-02a7-43ef-b45e-2cf4d2036f7d&s=J3fL%2F4tA5kpLZHlXYJViq54A88If1JhdAgE6v0fx9Kw%3D
E/HTTP-LOG: LOGGER : nel: {"report_to":"heroku-nel","max_age":3600,"success_fraction":0.005,"failure_fraction":0.05,"response_headers":["Via"]}
E/HTTP-LOG: LOGGER : x-powered-by: Express
E/HTTP-LOG: LOGGER : x-ratelimit-limit: 1000
E/HTTP-LOG: LOGGER : x-ratelimit-remaining: 999
E/HTTP-LOG: LOGGER : x-ratelimit-reset: 1701817120
E/HTTP-LOG: LOGGER : vary: Origin, Accept-Encoding
E/HTTP-LOG: LOGGER : access-control-allow-credentials: true
E/HTTP-LOG: LOGGER : cache-control: max-age=43200
E/HTTP-LOG: LOGGER : pragma: no-cache
E/HTTP-LOG: LOGGER : expires: -1
E/HTTP-LOG: LOGGER : x-content-type-options: nosniff
E/HTTP-LOG: LOGGER : etag: W/"134-QtF0EaiifRh1Z3cEspXIXV0RoQY"
E/HTTP-LOG: LOGGER : via: 1.1 vegur
E/HTTP-LOG: LOGGER : cf-cache-status: MISS
E/HTTP-LOG: LOGGER : server: cloudflare
E/HTTP-LOG: LOGGER : cf-ray: 830fe46908162626-NRT
E/HTTP-LOG: LOGGER : alt-svc: h3=":443"; ma=86400
E/HTTP-LOG: LOGGER : 
E/HTTP-LOG: LOGGER : [
      {
        "userId": 1,
        "id": 1,
        "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
        "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
      }
    ]
E/HTTP-LOG: LOGGER : <-- END HTTP (308-byte body)

 

반응형
Comments