Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MOD/#5] timber logging interceptor #6

Merged
merged 2 commits into from
Dec 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("]")