Use Timber instead of Android Log

This commit is contained in:
William Brawner 2023-09-26 20:23:03 -06:00
parent 2a1ab12c2e
commit 92495c06c4
Signed by: wbrawner
GPG key ID: 8FF12381C6C90D35
3 changed files with 23 additions and 26 deletions

View file

@ -56,6 +56,7 @@ dependencies {
implementation 'androidx.annotation:annotation:1.5.0'
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.1"
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
implementation 'com.jakewharton.timber:timber:5.0.1'
def okhttp_version = "4.10.0"
implementation "com.squareup.okhttp3:okhttp:$okhttp_version"
testImplementation "com.squareup.okhttp3:mockwebserver:$okhttp_version"

View file

@ -1,8 +1,8 @@
package com.wbrawner.plausible.android
import android.content.Context
import android.util.Log
import com.wbrawner.plausible.android.Plausible.init
import timber.log.Timber
import java.util.concurrent.atomic.AtomicReference
/**
@ -35,10 +35,8 @@ object Plausible {
?.let {
it.enable = enable
}
?: Log.w(
"Plausible",
"Ignoring call to enable(). Did you forget to call Plausible.init()?"
)
?: Timber.tag("Plausible")
.w("Ignoring call to enable(). Did you forget to call Plausible.init()?")
}
/**
@ -55,10 +53,8 @@ object Plausible {
?.let {
it.userAgent = userAgent
}
?: Log.w(
"Plausible",
"Ignoring call to setUserAgent(). Did you forget to call Plausible.init()?"
)
?: Timber.tag("Plausible")
.w("Ignoring call to setUserAgent(). Did you forget to call Plausible.init()?")
}
/**
@ -127,14 +123,10 @@ object Plausible {
?.let { config ->
client.event(config.domain, name, url, referrer, config.screenWidth, props)
}
?: Log.w(
"Plausible",
"Ignoring call to event(). Did you forget to call Plausible.init()?"
)
?: Timber.tag("Plausible")
.w("Ignoring call to event(). Did you forget to call Plausible.init()?")
}
?: Log.w(
"Plausible",
"Ignoring call to event(). Did you forget to call Plausible.init()?"
)
?: Timber.tag("Plausible")
.w("Ignoring call to event(). Did you forget to call Plausible.init()?")
}
}

View file

@ -1,17 +1,21 @@
package com.wbrawner.plausible.android
import android.net.Uri
import android.util.Log
import androidx.annotation.VisibleForTesting
import kotlinx.coroutines.*
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonPrimitive
import okhttp3.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import okhttp3.Call
import okhttp3.Callback
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import timber.log.Timber
import java.io.File
import java.io.IOException
import kotlin.coroutines.CoroutineContext
@ -138,7 +142,7 @@ internal class NetworkFirstPlausibleClient(
private suspend fun postEvent(event: Event) {
if (!config.enable) {
Log.w("Plausible", "Plausible disabled, not sending event: $event")
Timber.tag("Plausible").w("Plausible disabled, not sending event: $event")
return
}
val body = event.toJson().toRequestBody("application/json".toMediaType())
@ -160,7 +164,7 @@ internal class NetworkFirstPlausibleClient(
call.enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
Log.e("Plausible", "Failed to send event to backend", e)
Timber.tag("Plausible").e(e, "Failed to send event to backend")
continuation.resumeWithException(e)
}