From 130be9a960c5f32b4366e0dff9464d4f9c42e10b Mon Sep 17 00:00:00 2001 From: Krzysztof Borowy Date: Wed, 14 Dec 2022 03:53:30 +0100 Subject: [PATCH] feat: Add Logger to network client (#101) --- data/network/build.gradle.kts | 2 ++ .../common/network/di/NetworkModule.kt | 16 +++++++++++++++- gradle/libs.versions.toml | 3 +++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/data/network/build.gradle.kts b/data/network/build.gradle.kts index 19e37c30..26b85a26 100644 --- a/data/network/build.gradle.kts +++ b/data/network/build.gradle.kts @@ -46,8 +46,10 @@ kotlin { implementation(libs.io.ktor.client.serialization) implementation(libs.io.ktor.serialization.kotlinx.json) implementation(libs.io.ktor.client.content.negotiation) + implementation(libs.io.ktor.client.logging) implementation(libs.org.jetbrains.kotlinx.serialization.json) implementation(libs.io.insert.koin.core) + implementation(libs.io.github.aakira.napier) } } diff --git a/data/network/src/commonMain/kotlin/social/androiddev/common/network/di/NetworkModule.kt b/data/network/src/commonMain/kotlin/social/androiddev/common/network/di/NetworkModule.kt index 3df5ffb7..a5d3fef4 100644 --- a/data/network/src/commonMain/kotlin/social/androiddev/common/network/di/NetworkModule.kt +++ b/data/network/src/commonMain/kotlin/social/androiddev/common/network/di/NetworkModule.kt @@ -9,9 +9,14 @@ */ package social.androiddev.common.network.di +import io.github.aakira.napier.DebugAntilog +import io.github.aakira.napier.Napier import io.ktor.client.HttpClient import io.ktor.client.plugins.contentnegotiation.ContentNegotiation import io.ktor.client.plugins.defaultRequest +import io.ktor.client.plugins.logging.LogLevel +import io.ktor.client.plugins.logging.Logger +import io.ktor.client.plugins.logging.Logging import io.ktor.http.URLProtocol import io.ktor.serialization.kotlinx.json.json import kotlinx.serialization.json.Json @@ -26,9 +31,18 @@ import social.androiddev.common.network.MastodonApiKtor * network/api related classes */ val networkModule: Module = module { - singleOf { HttpClient { + install(Logging) { + logger = object : Logger { + override fun log(message: String) { + Napier.v(message, null, "HttpClient") + } + } + level = LogLevel.BODY + // todo: provide different antilog for release + Napier.base(DebugAntilog()) + } // install plugin so we can use type-safe data models for serialization in ktor install(ContentNegotiation) { json( diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5d225364..d01aeb71 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -20,6 +20,7 @@ org-xerial = "3.8.10.2" io-insert-koin = "3.2.0" com-russhwolf = "1.0.0-RC" kotlinx-serialization = "1.4.0" +io-github-aakira = "2.6.1" [libraries] # plugins @@ -40,6 +41,7 @@ io-ktor-client-content-negotiation = { module = "io.ktor:ktor-client-content-neg io-ktor-client-mock-jvm = { module = "io.ktor:ktor-client-mock-jvm", version.ref = "io-ktor" } io-ktor-client-mock = { module = "io.ktor:ktor-client-mock", version.ref = "io-ktor" } io-ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "io-ktor" } +io-ktor-client-logging = { module = "io.ktor:ktor-client-logging", version.ref = "io-ktor" } io-insert-koin-core = { module = "io.insert-koin:koin-core", version.ref = "io-insert-koin" } io-insert-koin-test = { module = "io.insert-koin:koin-test", version.ref = "io-insert-koin" } io-insert-koin-android = { module = "io.insert-koin:koin-android", version.ref = "io-insert-koin" } @@ -68,5 +70,6 @@ com-arkivanov-decompose-extensions-compose-jetbrains = { module = "com.arkivanov com-arkivanov-decompose-extensions-compose-jetpack = { module = "com.arkivanov.decompose:extensions-compose-jetpack", version.ref = "com-arkivanov-decompose" } multiplatform-settings = { module = "com.russhwolf:multiplatform-settings", version.ref = "com-russhwolf" } kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" } +io-github-aakira-napier = { module = "io.github.aakira:napier", version.ref = "io-github-aakira" } [plugins]