forked from customerio/customerio-android
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1c6872
commit 81eea4e
Showing
6 changed files
with
122 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
72
sdk/src/sharedTest/java/io/customer/sdk/util/LoggerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |