Add Napier logger library
This commit is contained in:
@@ -55,6 +55,8 @@ kotlin {
|
||||
|
||||
implementation(libs.koin.core)
|
||||
implementation(libs.koin.compose.viewmodel)
|
||||
|
||||
implementation(libs.logger.napier)
|
||||
}
|
||||
iosMain.dependencies {
|
||||
implementation(libs.ktor.client.darwin)
|
||||
|
||||
+4
@@ -2,10 +2,14 @@ package com.digitoolsolutions.app.torquevaultkmp
|
||||
|
||||
import android.app.Application
|
||||
import com.digitoolsolutions.app.torquevaultkmp.di.initKoin
|
||||
import io.github.aakira.napier.DebugAntilog
|
||||
import io.github.aakira.napier.Napier
|
||||
import org.koin.android.ext.koin.androidContext
|
||||
class MyApplication : Application() {
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
// Add logger lib
|
||||
Napier.base(DebugAntilog()) // NoOpAntilog() for release
|
||||
initKoin {
|
||||
androidContext(this@MyApplication)
|
||||
}
|
||||
|
||||
+11
@@ -11,6 +11,9 @@ import io.ktor.client.plugins.DefaultRequest
|
||||
import io.ktor.client.plugins.HttpSend
|
||||
import io.ktor.client.plugins.HttpTimeout
|
||||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
|
||||
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.plugins.plugin
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.http.HttpStatusCode
|
||||
@@ -34,6 +37,14 @@ actual fun createPlatformHttpClient(
|
||||
}
|
||||
}
|
||||
install(HttpTimeout) { requestTimeoutMillis = 30_000 }
|
||||
install(Logging) {
|
||||
logger = object : Logger {
|
||||
override fun log(message: String) {
|
||||
Napier.v(message, tag = "HTTP")
|
||||
}
|
||||
}
|
||||
level = LogLevel.ALL // set LogLevel.NONE for release
|
||||
}
|
||||
}
|
||||
|
||||
if (authApi != null) {
|
||||
|
||||
+1
-3
@@ -20,16 +20,14 @@ class AuthApi(
|
||||
) {
|
||||
suspend fun login(username: String, password: String): Boolean {
|
||||
return try {
|
||||
println(">>>>> Check LOGIN Api")
|
||||
val res: LoginResponseDto = client.post("$baseUrl${Endpoints.AUTH_TOKEN}") {
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(LoginRequestDto(username, password))
|
||||
}.body()
|
||||
println(res)
|
||||
tokenManager.saveTokens(res.access, res.refresh)
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
println(">>>>> [AuthApi] Exception: $e")
|
||||
println(">>>>> [AuthApi.kt] Exception: $e")
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.digitoolsolutions.app.torquevaultkmp.utils
|
||||
|
||||
import io.github.aakira.napier.Antilog
|
||||
import io.github.aakira.napier.LogLevel
|
||||
|
||||
class NoOpAntilog : Antilog() {
|
||||
override fun isEnable(priority: LogLevel, tag: String?): Boolean = false
|
||||
override fun performLog(priority: LogLevel, tag: String?, throwable: Throwable?, message: String?) {}
|
||||
}
|
||||
+4
@@ -2,9 +2,13 @@ package com.digitoolsolutions.app.torquevaultkmp
|
||||
|
||||
import androidx.compose.ui.window.ComposeUIViewController
|
||||
import com.digitoolsolutions.app.torquevaultkmp.di.initKoin
|
||||
import io.github.aakira.napier.DebugAntilog
|
||||
import io.github.aakira.napier.Napier
|
||||
|
||||
fun MainViewController() = ComposeUIViewController(
|
||||
configure = {
|
||||
// Add logger lib
|
||||
Napier.base(DebugAntilog()) // NoOpAntilog() for release
|
||||
initKoin()
|
||||
}
|
||||
) { App() }
|
||||
+13
-1
@@ -4,12 +4,16 @@ import com.digitoolsolutions.app.torquevaultkmp.data.network.api.AuthApi
|
||||
import com.digitoolsolutions.app.torquevaultkmp.data.storage.ReferKeys
|
||||
import com.digitoolsolutions.app.torquevaultkmp.data.storage.TokenManager
|
||||
import com.digitoolsolutions.app.torquevaultkmp.utils.ForceLogoutException
|
||||
import io.github.aakira.napier.Napier
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.darwin.Darwin
|
||||
import io.ktor.client.plugins.DefaultRequest
|
||||
import io.ktor.client.plugins.HttpSend
|
||||
import io.ktor.client.plugins.HttpTimeout
|
||||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
|
||||
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.plugins.plugin
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.http.HttpStatusCode
|
||||
@@ -33,6 +37,14 @@ actual fun createPlatformHttpClient(
|
||||
}
|
||||
}
|
||||
install(HttpTimeout) { requestTimeoutMillis = 30_000 }
|
||||
install(Logging) {
|
||||
logger = object : Logger {
|
||||
override fun log(message: String) {
|
||||
Napier.v(message, tag = "HTTP")
|
||||
}
|
||||
}
|
||||
level = LogLevel.ALL // set LogLevel.NONE for release
|
||||
}
|
||||
}
|
||||
|
||||
if (authApi != null) {
|
||||
@@ -44,7 +56,7 @@ actual fun createPlatformHttpClient(
|
||||
|
||||
val originalCall = execute(request)
|
||||
if (originalCall.response.status == HttpStatusCode.Unauthorized) {
|
||||
println(">>>>> [NetworkEngine.ios.kt] Access token expired, auto request refresh token...")
|
||||
Napier.d(">>>>> [NetworkEngine.ios.kt] Access token expired, auto request refresh token...")
|
||||
val newToken = authApi.refreshToken()
|
||||
if (newToken != null) {
|
||||
request.headers["Authorization"] = "Bearer $newToken"
|
||||
|
||||
Reference in New Issue
Block a user