Skip to content

Commit

Permalink
feat: set log level via SDK config
Browse files Browse the repository at this point in the history
  • Loading branch information
levibostian committed May 4, 2022
1 parent b1c6872 commit 81eea4e
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 10 deletions.
Expand Up @@ -8,6 +8,7 @@ import io.customer.sdk.CustomerIOConfig
import io.customer.sdk.data.model.Region
import io.customer.sdk.data.store.DeviceStore
import io.customer.sdk.di.CustomerIOComponent
import io.customer.sdk.util.CioLogLevel
import io.customer.sdk.util.JsonAdapter
import okhttp3.ResponseBody.Companion.toResponseBody
import org.junit.Before
Expand All @@ -29,7 +30,7 @@ abstract class BaseTest {
get() = ApplicationProvider.getApplicationContext()

protected val cioConfig: CustomerIOConfig
get() = CustomerIOConfig(siteId, "xyz", Region.EU, 100, null, true, true, 10, 30.0)
get() = CustomerIOConfig(siteId, "xyz", Region.EU, 100, null, true, true, 10, 30.0, CioLogLevel.DEBUG)

protected val deviceStore: DeviceStore = DeviceStoreStub().deviceStore

Expand Down
10 changes: 9 additions & 1 deletion sdk/src/main/java/io/customer/sdk/CustomerIO.kt
Expand Up @@ -9,6 +9,7 @@ import io.customer.sdk.data.communication.CustomerIOUrlHandler
import io.customer.sdk.data.model.Region
import io.customer.sdk.data.request.MetricEvent
import io.customer.sdk.di.CustomerIOComponent
import io.customer.sdk.util.CioLogLevel

/**
* Allows mocking of [CustomerIO] for your automated tests in your project. Mock [CustomerIO] to assert your code is calling functions
Expand Down Expand Up @@ -96,6 +97,7 @@ class CustomerIO internal constructor(
private var urlHandler: CustomerIOUrlHandler? = null
private var shouldAutoRecordScreenViews: Boolean = false
private var autoTrackDeviceAttributes: Boolean = true
private var logLevel = CioLogLevel.ERROR

private lateinit var activityLifecycleCallback: CustomerIOActivityLifecycleCallbacks

Expand All @@ -119,6 +121,11 @@ class CustomerIO internal constructor(
return this
}

fun setLogLevel(level: CioLogLevel): Builder {
this.logLevel = level
return this
}

/**
* Override url/deep link handling
*
Expand Down Expand Up @@ -148,7 +155,8 @@ class CustomerIO internal constructor(
autoTrackScreenViews = shouldAutoRecordScreenViews,
autoTrackDeviceAttributes = autoTrackDeviceAttributes,
backgroundQueueMinNumberOfTasks = 10,
backgroundQueueSecondsDelay = 30.0
backgroundQueueSecondsDelay = 30.0,
logLevel = logLevel
)

val diGraph = CustomerIOComponent(sdkConfig = config, context = appContext)
Expand Down
5 changes: 3 additions & 2 deletions sdk/src/main/java/io/customer/sdk/CustomerIOConfig.kt
Expand Up @@ -2,6 +2,7 @@ package io.customer.sdk

import io.customer.sdk.data.communication.CustomerIOUrlHandler
import io.customer.sdk.data.model.Region
import io.customer.sdk.util.CioLogLevel

data class CustomerIOConfig(
val siteId: String,
Expand All @@ -21,6 +22,6 @@ data class CustomerIOConfig(
* The number of seconds to delay running queue after a task has been added to it.
* We do not recommend modifying this value because it impacts battery life of mobile device.
*/
val backgroundQueueSecondsDelay: Double

val backgroundQueueSecondsDelay: Double,
val logLevel: CioLogLevel
)
Expand Up @@ -75,7 +75,7 @@ class CustomerIOComponent(
get() = override() ?: QueueRunRequestImpl(queueRunner, queueStorage, logger, queueQueryRunner)

val logger: Logger
get() = override() ?: LogcatLogger()
get() = override() ?: LogcatLogger(sdkConfig)

internal val cioHttpClient: TrackingHttpClient
get() = override() ?: RetrofitTrackingHttpClient(buildRetrofitApi(), httpRequestRunner)
Expand Down
40 changes: 35 additions & 5 deletions sdk/src/main/java/io/customer/sdk/util/Logger.kt
@@ -1,27 +1,57 @@
package io.customer.sdk.util

import android.util.Log
import io.customer.sdk.CustomerIOConfig

interface Logger {
fun info(message: String)
fun debug(message: String)
fun error(message: String)
}

class LogcatLogger : Logger {
enum class CioLogLevel {
NONE,
ERROR,
INFO,
DEBUG;

fun shouldLog(levelForMessage: CioLogLevel): Boolean {
return when (this) {
NONE -> false
ERROR -> levelForMessage == ERROR
INFO -> levelForMessage == ERROR || levelForMessage == INFO
DEBUG -> true
}
}
}

class LogcatLogger(
private val sdkConfig: CustomerIOConfig
) : Logger {

private val tag = "[CIO]"

override fun info(message: String) {
Log.i(tag, message)
runIfMeetsLogLevelCriteria(CioLogLevel.INFO) {
Log.i(tag, message)
}
}

override fun debug(message: String) {
// TODO set log level via sdkconfig
Log.d(tag, message)
runIfMeetsLogLevelCriteria(CioLogLevel.DEBUG) {
Log.d(tag, message)
}
}

override fun error(message: String) {
Log.e(tag, message)
runIfMeetsLogLevelCriteria(CioLogLevel.ERROR) {
Log.e(tag, message)
}
}

private fun runIfMeetsLogLevelCriteria(levelForMessage: CioLogLevel, block: () -> Unit) {
val shouldLog = sdkConfig.logLevel.shouldLog(levelForMessage)

if (shouldLog) block()
}
}
72 changes: 72 additions & 0 deletions sdk/src/sharedTest/java/io/customer/sdk/util/LoggerTest.kt
@@ -0,0 +1,72 @@
package io.customer.sdk.util

import androidx.test.ext.junit.runners.AndroidJUnit4
import io.customer.common_test.BaseTest
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class LoggerTest : BaseTest() {

// Test log levels

@Test
fun shouldLog_givenNone() {
val configLogLevelSet = CioLogLevel.NONE

assertShouldLog(
configLogLevelSet,
error = false,
info = false,
debug = false
)
}

@Test
fun shouldLog_givenError() {
val configLogLevelSet = CioLogLevel.ERROR

assertShouldLog(
configLogLevelSet,
error = true,
info = false,
debug = false
)
}

@Test
fun shouldLog_givenInfo() {
val configLogLevelSet = CioLogLevel.INFO

assertShouldLog(
configLogLevelSet,
error = true,
info = true,
debug = false
)
}

@Test
fun shouldLog_givenDebug() {
val configLogLevelSet = CioLogLevel.DEBUG

assertShouldLog(
configLogLevelSet,
error = true,
info = true,
debug = true
)
}

private fun assertShouldLog(
levelSetBySdkConfig: CioLogLevel,
error: Boolean,
info: Boolean,
debug: Boolean
) {
levelSetBySdkConfig.shouldLog(CioLogLevel.ERROR) shouldBeEqualTo error
levelSetBySdkConfig.shouldLog(CioLogLevel.INFO) shouldBeEqualTo info
levelSetBySdkConfig.shouldLog(CioLogLevel.DEBUG) shouldBeEqualTo debug
}
}

0 comments on commit 81eea4e

Please sign in to comment.