Skip to content

Commit

Permalink
feat(logger): added wrapper information to logs
Browse files Browse the repository at this point in the history
SUITEDEV-28066

Co-authored-by: LasOri <[email protected]>
Co-authored-by: megamegax <[email protected]>
Co-authored-by: kovacszsoltizsolt <[email protected]>
  • Loading branch information
4 people committed Jun 8, 2021
1 parent 9867563 commit a5e427f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 24 deletions.
20 changes: 11 additions & 9 deletions core/src/androidTest/java/com/emarsys/core/util/log/LoggerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import com.emarsys.testUtil.TimeoutUtils
import com.emarsys.testUtil.mockito.ThreadSpy
import com.nhaarman.mockitokotlin2.*
import io.kotlintest.shouldBe
import org.junit.*
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.mockito.ArgumentCaptor
import java.util.concurrent.CountDownLatch
Expand Down Expand Up @@ -59,13 +62,13 @@ class LoggerTest {
mockLogLevelStorage = mock()

loggerInstance = Logger(
coreSdkHandler,
shardRepositoryMock,
timestampProviderMock,
uuidProviderMock,
mockLogLevelStorage,
false,
mock()
coreSdkHandler,
shardRepositoryMock,
timestampProviderMock,
uuidProviderMock,
mockLogLevelStorage,
false,
mock()
)
loggerMock = mock()

Expand Down Expand Up @@ -171,7 +174,6 @@ class LoggerTest {
verify(loggerMock).handleLog(LogLevel.INFO, logEntry)
}

@Ignore
@Test
fun testLog_doesNotLogAnything_ifDependencyInjection_isNotSetup() {
DependencyInjection.tearDown()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,33 @@ import org.junit.Test
class LogEntryKtTest {

@Test
fun testDataWithLevel() {
fun testToData() {
val logEntry: LogEntry = mock {
on { data } doReturn mapOf(
"testKey" to "testValue",
"testKey2" to "testValue2"
)
}
val result = logEntry.dataWithLogLevel(LogLevel.INFO, "testThreadName")
val result = logEntry.toData(LogLevel.INFO, "testThreadName", "flutter")

result shouldBe mapOf(
"testKey" to "testValue",
"testKey2" to "testValue2",
"level" to "INFO",
"thread" to "testThreadName",
"wrapper" to "flutter"
)
}

@Test
fun testToData_shouldExcludeWrapper_whenWrapperInfo_isNull() {
val logEntry: LogEntry = mock {
on { data } doReturn mapOf(
"testKey" to "testValue",
"testKey2" to "testValue2"
)
}
val result = logEntry.toData(LogLevel.INFO, "testThreadName", null)

result shouldBe mapOf(
"testKey" to "testValue",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.emarsys.core.provider.wrapper

object WrapperInfoContainer {

var wrapperInfo: String? = null
}
17 changes: 9 additions & 8 deletions core/src/main/java/com/emarsys/core/util/log/Logger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ import com.emarsys.core.endpoint.Endpoint.LOG_URL
import com.emarsys.core.handler.CoreSdkHandler
import com.emarsys.core.provider.timestamp.TimestampProvider
import com.emarsys.core.provider.uuid.UUIDProvider
import com.emarsys.core.provider.wrapper.WrapperInfoContainer
import com.emarsys.core.shard.ShardModel
import com.emarsys.core.storage.StringStorage
import com.emarsys.core.util.log.LogLevel.*
import com.emarsys.core.util.log.entry.*

@Mockable
class Logger(
private val coreSdkHandler: CoreSdkHandler,
private val shardRepository: Repository<ShardModel, SqlSpecification>,
private val timestampProvider: TimestampProvider,
private val uuidProvider: UUIDProvider,
private val logLevelStorage: StringStorage,
private val verboseConsoleLoggingEnabled: Boolean,
private val context: Context
private val coreSdkHandler: CoreSdkHandler,
private val shardRepository: Repository<ShardModel, SqlSpecification>,
private val timestampProvider: TimestampProvider,
private val uuidProvider: UUIDProvider,
private val logLevelStorage: StringStorage,
private val verboseConsoleLoggingEnabled: Boolean,
private val context: Context
) {

companion object {
Expand Down Expand Up @@ -123,7 +124,7 @@ class Logger(
coreSdkHandler.post {
val shard = ShardModel.Builder(timestampProvider, uuidProvider)
.type(logEntry.topic)
.payloadEntries(logEntry.dataWithLogLevel(logLevel, currentThreadName))
.payloadEntries(logEntry.toData(logLevel, currentThreadName, WrapperInfoContainer.wrapperInfo))
.build()
shardRepository.add(shard)
onCompleted?.invoke()
Expand Down
15 changes: 10 additions & 5 deletions core/src/main/java/com/emarsys/core/util/log/entry/LogEntry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ interface LogEntry {
val data: Map<String, Any?>
}

fun LogEntry.dataWithLogLevel(logLevel: LogLevel, currentThreadName: String) =
mutableMapOf<String, Any?>(
"level" to logLevel.name,
"thread" to currentThreadName
).apply { putAll(data) }
fun LogEntry.toData(logLevel: LogLevel, currentThreadName: String, wrapperInfo: String?) =
mutableMapOf<String, Any?>(
"level" to logLevel.name,
"thread" to currentThreadName
).apply {
if (wrapperInfo != null) {
put("wrapper", wrapperInfo)
}
putAll(data)
}

fun LogEntry.asString(): String {
return "topic='$topic', data=$data"
Expand Down

0 comments on commit a5e427f

Please sign in to comment.