Skip to content

Commit

Permalink
feat: log network headers with timber
Browse files Browse the repository at this point in the history
resolves #408
  • Loading branch information
David Ly committed Sep 2, 2023
1 parent d83065c commit fd55c6b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 2 additions & 0 deletions data-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ dependencies {
implementation(libs.room.ktx)
implementation(libs.room.paging)

implementation(libs.timber)

testImplementation(libs.androidx.test.junit)
testImplementation(libs.junit)
testImplementation(libs.bundles.kotlinx.coroutines)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import io.ktor.client.HttpClient
import io.ktor.client.call.NoTransformationFoundException
import io.ktor.client.plugins.ClientRequestException
import io.ktor.client.plugins.HttpResponseValidator
import io.ktor.client.plugins.logging.LogLevel
import io.ktor.client.plugins.logging.Logger
import io.ktor.client.plugins.logging.Logging
import io.ktor.client.statement.bodyAsText
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpStatusCode
import javax.inject.Singleton
import ly.david.data.BuildConfig
Expand All @@ -20,6 +24,7 @@ import ly.david.data.network.api.MusicBrainzApi
import ly.david.data.spotify.api.SpotifyApi
import ly.david.data.spotify.api.auth.SpotifyAuthApi
import ly.david.data.spotify.api.auth.SpotifyAuthState
import timber.log.Timber

@Module
@InstallIn(SingletonComponent::class)
Expand All @@ -34,13 +39,23 @@ object NetworkModule {
handleRecoverableException(exception)
}
}

install(Logging) {
level = LogLevel.HEADERS
logger = object : Logger {
override fun log(message: String) {
Timber.d(message)
}
}
sanitizeHeader { header -> header == HttpHeaders.Authorization }
}
}
}

private suspend fun handleRecoverableException(exception: Throwable) {
when (exception) {
is NoTransformationFoundException -> {
// TODO: should be logged
Timber.e(exception)
throw RecoverableNetworkException("Requested json but got xml")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import io.ktor.client.HttpClient
import io.ktor.client.HttpClientConfig
import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.plugins.logging.LogLevel
import io.ktor.client.plugins.logging.Logging
import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.json.Json

Expand All @@ -14,10 +12,6 @@ object ApiHttpClient {
return HttpClient(OkHttp) {
expectSuccess = true

install(Logging) {
level = LogLevel.ALL
// TODO: logger
}
install(ContentNegotiation) {
json(
Json {
Expand Down

0 comments on commit fd55c6b

Please sign in to comment.