Skip to content

Commit

Permalink
Merge pull request #6 from Team-Going/mod/#5-timber-logging-interceptor
Browse files Browse the repository at this point in the history
[MOD/#5] timber logging interceptor
  • Loading branch information
chattymin authored Dec 28, 2023
2 parents efa61d7 + 4456f4e commit 4664dcc
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions app/src/main/java/com/going/going/di/RetrofitModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.json.JSONObject
import retrofit2.Converter
import retrofit2.Retrofit
import timber.log.Timber
import javax.inject.Singleton

@Module
Expand All @@ -34,14 +36,26 @@ object RetrofitModule {

@Provides
@Singleton
fun provideHttpLoggingInterceptor(): Interceptor = HttpLoggingInterceptor().apply {
fun provideHttpLoggingInterceptor(): Interceptor = HttpLoggingInterceptor { message ->
when {
message.isJsonObject() ->
Timber.tag("okhttp").d(JSONObject(message).toString(4))

message.isJsonArray() ->
Timber.tag("okhttp").d(JSONObject(message).toString(4))

else -> {
Timber.tag("okhttp").d("CONNECTION INFO -> $message")
}
}
}.apply {
level = HttpLoggingInterceptor.Level.BODY
}

@Provides
@Singleton
fun provideOkHttpClient(
loggingInterceptor: Interceptor
loggingInterceptor: Interceptor,
): OkHttpClient = OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build()
Expand All @@ -56,4 +70,7 @@ object RetrofitModule {
.client(client)
.addConverterFactory(factory)
.build()
}
}

fun String?.isJsonObject(): Boolean = this?.startsWith("{") == true && this.endsWith("}")
fun String?.isJsonArray(): Boolean = this?.startsWith("[") == true && this.endsWith("]")

0 comments on commit 4664dcc

Please sign in to comment.