From a5e427f263070c5648b163edc5b2c7c6797f1278 Mon Sep 17 00:00:00 2001 From: dschuppa Date: Tue, 8 Jun 2021 13:22:20 +0200 Subject: [PATCH] feat(logger): added wrapper information to logs SUITEDEV-28066 Co-authored-by: LasOri <24588073+LasOri@users.noreply.github.com> Co-authored-by: megamegax Co-authored-by: kovacszsoltizsolt <22084766+kovacszsoltizsolt@users.noreply.github.com> --- .../com/emarsys/core/util/log/LoggerTest.kt | 20 ++++++++-------- .../core/util/log/entry/LogEntryKtTest.kt | 23 +++++++++++++++++-- .../provider/wrapper/WrapperInfoContainer.kt | 6 +++++ .../java/com/emarsys/core/util/log/Logger.kt | 17 +++++++------- .../emarsys/core/util/log/entry/LogEntry.kt | 15 ++++++++---- 5 files changed, 57 insertions(+), 24 deletions(-) create mode 100644 core/src/main/java/com/emarsys/core/provider/wrapper/WrapperInfoContainer.kt diff --git a/core/src/androidTest/java/com/emarsys/core/util/log/LoggerTest.kt b/core/src/androidTest/java/com/emarsys/core/util/log/LoggerTest.kt index 94e4c6931..2cc85414d 100644 --- a/core/src/androidTest/java/com/emarsys/core/util/log/LoggerTest.kt +++ b/core/src/androidTest/java/com/emarsys/core/util/log/LoggerTest.kt @@ -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 @@ -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() @@ -171,7 +174,6 @@ class LoggerTest { verify(loggerMock).handleLog(LogLevel.INFO, logEntry) } - @Ignore @Test fun testLog_doesNotLogAnything_ifDependencyInjection_isNotSetup() { DependencyInjection.tearDown() diff --git a/core/src/androidTest/java/com/emarsys/core/util/log/entry/LogEntryKtTest.kt b/core/src/androidTest/java/com/emarsys/core/util/log/entry/LogEntryKtTest.kt index d94edff12..f0429aa81 100644 --- a/core/src/androidTest/java/com/emarsys/core/util/log/entry/LogEntryKtTest.kt +++ b/core/src/androidTest/java/com/emarsys/core/util/log/entry/LogEntryKtTest.kt @@ -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", diff --git a/core/src/main/java/com/emarsys/core/provider/wrapper/WrapperInfoContainer.kt b/core/src/main/java/com/emarsys/core/provider/wrapper/WrapperInfoContainer.kt new file mode 100644 index 000000000..830c25bd4 --- /dev/null +++ b/core/src/main/java/com/emarsys/core/provider/wrapper/WrapperInfoContainer.kt @@ -0,0 +1,6 @@ +package com.emarsys.core.provider.wrapper + +object WrapperInfoContainer { + + var wrapperInfo: String? = null +} \ No newline at end of file diff --git a/core/src/main/java/com/emarsys/core/util/log/Logger.kt b/core/src/main/java/com/emarsys/core/util/log/Logger.kt index b769abf6c..6ba81a6ea 100644 --- a/core/src/main/java/com/emarsys/core/util/log/Logger.kt +++ b/core/src/main/java/com/emarsys/core/util/log/Logger.kt @@ -12,6 +12,7 @@ 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.* @@ -19,13 +20,13 @@ import com.emarsys.core.util.log.entry.* @Mockable class Logger( - private val coreSdkHandler: CoreSdkHandler, - private val shardRepository: Repository, - 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, + private val timestampProvider: TimestampProvider, + private val uuidProvider: UUIDProvider, + private val logLevelStorage: StringStorage, + private val verboseConsoleLoggingEnabled: Boolean, + private val context: Context ) { companion object { @@ -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() diff --git a/core/src/main/java/com/emarsys/core/util/log/entry/LogEntry.kt b/core/src/main/java/com/emarsys/core/util/log/entry/LogEntry.kt index e29b374e3..c97120812 100644 --- a/core/src/main/java/com/emarsys/core/util/log/entry/LogEntry.kt +++ b/core/src/main/java/com/emarsys/core/util/log/entry/LogEntry.kt @@ -7,11 +7,16 @@ interface LogEntry { val data: Map } -fun LogEntry.dataWithLogLevel(logLevel: LogLevel, currentThreadName: String) = - mutableMapOf( - "level" to logLevel.name, - "thread" to currentThreadName - ).apply { putAll(data) } +fun LogEntry.toData(logLevel: LogLevel, currentThreadName: String, wrapperInfo: String?) = + mutableMapOf( + "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"