Skip to content

Commit

Permalink
Merge pull request #120 from Parsely/use_android_logging
Browse files Browse the repository at this point in the history
Use `android.util.Log` for logging
  • Loading branch information
wzieba authored Jan 13, 2025
2 parents dbb8f5e + a44209d commit d1c4b4f
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.parsely.parselyandroid
import android.content.Context
import android.provider.Settings
import com.google.android.gms.ads.identifier.AdvertisingIdClient
import com.parsely.parselyandroid.Logging.log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

Expand All @@ -19,7 +18,7 @@ internal class AdvertisementIdProvider(
try {
adKey = AdvertisingIdClient.getAdvertisingIdInfo(context).id
} catch (e: Exception) {
log("No Google play services or error!")
Log.e("No Google play services or error!", e)
}
}
}
Expand All @@ -41,7 +40,7 @@ internal class AndroidIdProvider(private val context: Context) : IdProvider {
} catch (ex: Exception) {
null
}
log(String.format("Android ID: %s", uuid))
Log.d("Android ID: $uuid")
return uuid
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.parsely.parselyandroid

import android.os.Build
import com.parsely.parselyandroid.Logging.log

internal interface DeviceInfoRepository{
fun collectDeviceInfo(): Map<String, String>
Expand All @@ -25,7 +24,7 @@ internal open class AndroidDeviceInfoRepository(
dInfo["parsely_site_uuid"] = parselySiteUuid
dInfo["manufacturer"] = Build.MANUFACTURER
dInfo["os"] = "android"
dInfo["os_version"] = String.format("%d", Build.VERSION.SDK_INT)
dInfo["os_version"] = Build.VERSION.SDK_INT.toString()

return dInfo
}
Expand All @@ -35,12 +34,12 @@ internal open class AndroidDeviceInfoRepository(
val adKey = advertisementIdProvider.provide()
val androidId = androidIdProvider.provide()

log("adkey is: %s, uuid is %s", adKey, androidId)
Log.d("adkey is: $adKey, uuid is $androidId")

return if (adKey != null) {
adKey
} else {
log("falling back to device uuid")
Log.d("falling back to device uuid")
androidId .orEmpty()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.parsely.parselyandroid

import com.parsely.parselyandroid.Logging.log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -62,7 +61,7 @@ internal class EngagementManager(
val event: MutableMap<String, Any> = HashMap(
baseEvent
)
log(String.format("Enqueuing %s event.", event["action"]))
Log.d("Enqueuing ${event["action"]} event.")

// Update `ts` for the event since it's happening right now.
val baseEventData = (event["data"] as Map<String, Any>?)!!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.parsely.parselyandroid

import com.parsely.parselyandroid.Logging.log

internal class EventsBuilder(
private val deviceInfoRepository: DeviceInfoRepository,
private val initializationSiteId: String,
Expand All @@ -27,7 +25,7 @@ internal class EventsBuilder(
uuid: String,
siteIdSource: SiteIdSource,
): Map<String, Any> {
log("buildEvent called for %s/%s", action, url)
Log.d("buildEvent called for $action/$url")

// Main event info
val event: MutableMap<String, Any> = HashMap()
Expand Down
16 changes: 7 additions & 9 deletions parsely/src/main/java/com/parsely/parselyandroid/FlushQueue.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.parsely.parselyandroid

import com.parsely.parselyandroid.JsonSerializer.toParselyEventsPayload
import com.parsely.parselyandroid.Logging.log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
Expand All @@ -19,7 +18,7 @@ internal class FlushQueue(

operator fun invoke(skipSendingEvents: Boolean) {
if (!connectivityStatusProvider.isReachable()) {
log("Network unreachable. Not flushing.")
Log.d("Network unreachable. Not flushing.")
return
}
scope.launch {
Expand All @@ -33,22 +32,21 @@ internal class FlushQueue(

val jsonPayload = toParselyEventsPayload(eventsToSend)
if (skipSendingEvents) {
log("Debug mode on. Not sending to Parse.ly. Otherwise, would sent ${eventsToSend.size} events: $jsonPayload")
Log.d("Debug mode on. Not sending to Parse.ly. Otherwise, would sent ${eventsToSend.size} events: $jsonPayload")
repository.remove(eventsToSend)
return@launch
}
log("Sending request with %d events", eventsToSend.size)
log("POST Data %s", jsonPayload)
log("Requested %s", ParselyTrackerInternal.ROOT_URL)
Log.d("Sending request with ${eventsToSend.size} events")
Log.d("POST Data $jsonPayload")
Log.d("Requested ${ParselyTrackerInternal.ROOT_URL}")
restClient.send(jsonPayload)
.fold(
onSuccess = {
log("Pixel request success")
Log.i("Pixel request success")
repository.remove(eventsToSend)
},
onFailure = {
log("Pixel request exception")
log(it.toString())
Log.e("Pixel request exception", it)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.parsely.parselyandroid

import com.parsely.parselyandroid.Logging.log
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
Expand All @@ -23,7 +22,7 @@ internal class InMemoryBuffer(
while (isActive) {
mutex.withLock {
if (buffer.isNotEmpty()) {
log("Persisting ${buffer.size} events")
Log.d("Persisting ${buffer.size} events")
localStorageRepository.insertEvents(buffer)
buffer.clear()
}
Expand All @@ -36,7 +35,7 @@ internal class InMemoryBuffer(
fun add(event: Map<String, Any>) {
coroutineScope.launch {
mutex.withLock {
log("Event added to buffer")
Log.d("Event added to buffer")
buffer.add(event)
onEventAddedListener()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.parsely.parselyandroid

import android.content.Context
import com.parsely.parselyandroid.Logging.log
import java.io.EOFException
import java.io.FileNotFoundException
import java.io.ObjectInputStream
Expand Down Expand Up @@ -35,7 +34,7 @@ internal class LocalStorageRepository(private val context: Context) : QueueRepos
oos.close()
fos.close()
} catch (ex: Exception) {
log("Exception thrown during queue serialization: %s", ex.toString())
Log.e("Exception thrown during queue serialization", ex)
}
}

Expand All @@ -53,10 +52,7 @@ internal class LocalStorageRepository(private val context: Context) : QueueRepos
} catch (ex: FileNotFoundException) {
// Nothing to do here. Means there was no saved queue.
} catch (ex: Exception) {
log(
"Exception thrown during queue deserialization: %s",
ex.toString()
)
Log.e("Exception thrown during queue deserialization", ex)
}
return storedQueue
}
Expand Down
34 changes: 34 additions & 0 deletions parsely/src/main/java/com/parsely/parselyandroid/Log.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.parsely.parselyandroid

import android.util.Log as AndroidLog

internal object AndroidLogWrapper : Log {

private const val TAG = "Parsely"

override fun i(message: String) {
AndroidLog.i(TAG, message)
}

override fun d(message: String) {
AndroidLog.d(TAG, message)
}

override fun e(message: String, throwable: Throwable?) {
AndroidLog.e(TAG, message, throwable)
}
}

internal interface Log {
fun i(message: String)
fun d(message: String)
fun e(message: String, throwable: Throwable?)

companion object {
var instance: Log = AndroidLogWrapper

fun i(message: String) = instance.i(message)
fun d(message: String) = instance.d(message)
fun e(message: String, throwable: Throwable? = null) = instance.e(message, throwable)
}
}
17 changes: 0 additions & 17 deletions parsely/src/main/java/com/parsely/parselyandroid/Logging.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal class ParselyTrackerInternal internal constructor(
inMemoryBuffer = InMemoryBuffer(sdkScope, localStorageRepository) {
if (!flushTimerIsActive()) {
startFlushTimer()
Logging.log("Flush flushTimer set to %ds", flushManager.intervalMillis / 1000)
Log.d("Flush flushTimer set to ${flushManager.intervalMillis / 1000}")
}
}
flushQueue = FlushQueue(
Expand Down Expand Up @@ -92,7 +92,7 @@ internal class ParselyTrackerInternal internal constructor(
siteIdSource: SiteIdSource,
) {
if (url.isBlank()) {
Logging.log("url cannot be empty")
Log.e("url cannot be empty")
return
}

Expand All @@ -119,12 +119,12 @@ internal class ParselyTrackerInternal internal constructor(
siteIdSource: SiteIdSource,
) {
if (url.isBlank()) {
Logging.log("url cannot be empty")
Log.e("url cannot be empty")
return
}
val pageViewUuid = lastPageviewUuid
if (pageViewUuid == null) {
Logging.log("engagement session cannot start without calling trackPageview first")
Log.e("engagement session cannot start without calling trackPageview first")
return
}

Expand All @@ -147,7 +147,7 @@ internal class ParselyTrackerInternal internal constructor(
override fun stopEngagement() {
engagementManager?.let {
it.stop()
Logging.log("Engagement session has been stopped")
Log.d("Engagement session has been stopped")
}
engagementManager = null
}
Expand All @@ -160,7 +160,7 @@ internal class ParselyTrackerInternal internal constructor(
siteIdSource: SiteIdSource,
) {
if (url.isBlank()) {
Logging.log("url cannot be empty")
Log.e("url cannot be empty")
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal class EventsBuilderTest {
TEST_SITE_ID,
clock
)
Log.instance = FakeLog
}

@Test
Expand Down
7 changes: 7 additions & 0 deletions parsely/src/test/java/com/parsely/parselyandroid/FakeLog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.parsely.parselyandroid

internal object FakeLog : Log {
override fun i(message: String) = println(message)
override fun d(message: String) = println(message)
override fun e(message: String, throwable: Throwable?) = println(message + throwable?.message)
}

0 comments on commit d1c4b4f

Please sign in to comment.