1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Retrofit2でログ出力

Posted at

はじめに

Retrofit2でデバッグ用にログ出力したかったので、使い方を自分用にメモ

参照元

実装

build.gradleに依存関係を追加

build.gradle
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'

loggingをRetrofitに設定

Retrofitのインスタンス作成時にクライアントとして、loggerを設定したhttpClientを渡す

val logger = HttpLoggingInterceptor()
logger.level = HttpLoggingInterceptor.Level.BODY

val httpClient = OkHttpClient.Builder()
httpClient.addInterceptor(logger)

val retrofit = Retrofit.Builder()
    .baseUrl(baseUrl)
    .addConverterFactory(gsonFactory)
    .client(httpClient.build())
    .build()

ログレベル

logger.levelで渡しているlevelには4種あり、出力の詳細さが変わる

NONE

ログを出力しない

BASIC

リクエストラインとレスポンスラインのみを出力

HEADER

BASICのログに加えて
リクエストヘッダとレスポンスヘッダが出力される

BODY

HEADERのログに加えて
リクエストボディとレスポンスボディが出力される

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?