Skip to content

Commit

Permalink
RUMM-55 Add the Log loggerName property
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusc83 committed Dec 6, 2019
1 parent e957fd8 commit e5bf9bf
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ internal fun randomLog(forge: Forge): Log {
networkInfo = NetworkInfo(
carrierName = forge.anAlphaNumericalString(),
carrierId = forge.anInt()
)
),
loggerName = forge.anAlphabeticalString()
)
}

Expand Down
15 changes: 14 additions & 1 deletion dd-sdk-android/src/main/kotlin/com/datadog/android/log/Logger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ private constructor(
val serviceName: String,
val datadogLogsEnabled: Boolean,
val logcatLogsEnabled: Boolean,
val loggerName: String,
private val logWriter: LogWriter,
internal val networkInfoProvider: NetworkInfoProvider?
) {
Expand Down Expand Up @@ -122,6 +123,7 @@ private constructor(

private var logStrategy: LogStrategy? = null
private var networkInfoProvider: NetworkInfoProvider? = null
private var loggerName: String = Datadog.packageName

/**
* Builds a [Logger] based on the current state of this Builder.
Expand All @@ -133,6 +135,7 @@ private constructor(
serviceName = serviceName,
datadogLogsEnabled = datadogLogsEnabled,
logcatLogsEnabled = logcatLogsEnabled,
loggerName = loggerName,
networkInfoProvider = if (networkInfoEnabled && datadogLogsEnabled) {
networkInfoProvider ?: Datadog.getNetworkInfoProvider()
} else null
Expand Down Expand Up @@ -185,6 +188,15 @@ private constructor(
return this
}

/**
* Use this if you want to set a custom name for this logger.
* @param name the logger custom name (default = application package name)
*/
fun setLoggerName(name: String): Builder {
loggerName = name
return this
}

private fun overrideLogStrategy(strategy: LogStrategy): Builder {
logStrategy = strategy
return this
Expand Down Expand Up @@ -366,7 +378,8 @@ private constructor(
throwable = throwable,
attributes = attributes.toMap(),
tags = tags.toList(),
networkInfo = networkInfoProvider?.getLatestNetworkInfos()
networkInfo = networkInfoProvider?.getLatestNetworkInfos(),
loggerName = loggerName
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ internal data class Log(
val attributes: Map<String, Any?>,
val tags: List<String>,
val throwable: Throwable?,
val networkInfo: NetworkInfo?
val networkInfo: NetworkInfo?,
val loggerName: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ internal interface LogStrategy {
internal const val TAG_ERROR_MESSAGE = "error.message"
internal const val TAG_ERROR_STACK = "error.stack"

// THREAD RELATED TAGS
internal const val TAG_LOGGER_NAME = "logger.name"

// ANDROID SPECIFIC TAGS
internal const val TAG_NETWORK = "network"
internal const val TAG_NETWORK_CONNECTIVITY = "connectivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ internal class LogFileWriter(
jsonLog.addProperty(LogStrategy.TAG_DATE, formattedDate)

// Network Infos
addLogNetworkInfo(log, jsonLog)

// Custom Attributes
addLogAttributes(log, jsonLog)

// Tags
addLogTags(log, jsonLog)

// Throwable
addLogThrowable(log, jsonLog)

return jsonLog.toString()
}

private fun addLogNetworkInfo(
log: Log,
jsonLog: JsonObject
) {
val info = log.networkInfo
if (info != null) {
val network = JsonObject()
Expand All @@ -96,8 +114,34 @@ internal class LogFileWriter(
}
jsonLog.add(LogStrategy.TAG_NETWORK, network)
}
}

// Custom Attributes
private fun addLogThrowable(
log: Log,
jsonLog: JsonObject
) {
log.throwable?.let {
jsonLog.addProperty(LogStrategy.TAG_LOGGER_NAME, log.loggerName)
val sw = StringWriter()
it.printStackTrace(PrintWriter(sw))
jsonLog.addProperty(LogStrategy.TAG_ERROR_KIND, it.javaClass.simpleName)
jsonLog.addProperty(LogStrategy.TAG_ERROR_MESSAGE, it.message)
jsonLog.addProperty(LogStrategy.TAG_ERROR_STACK, sw.toString())
}
}

private fun addLogTags(
log: Log,
jsonLog: JsonObject
) {
val tags = log.tags.joinToString(",")
jsonLog.addProperty(LogStrategy.TAG_DATADOG_TAGS, tags)
}

private fun addLogAttributes(
log: Log,
jsonLog: JsonObject
) {
log.attributes
.filter { it.key.isNotBlank() && it.key !in LogStrategy.reservedAttributes }
.forEach {
Expand All @@ -114,21 +158,6 @@ internal class LogFileWriter(
}
jsonLog.add(it.key, jsonValue)
}

// Tags
val tags = log.tags.joinToString(",")
jsonLog.addProperty(LogStrategy.TAG_DATADOG_TAGS, tags)

// Throwable
log.throwable?.let {
val sw = StringWriter()
it.printStackTrace(PrintWriter(sw))
jsonLog.addProperty(LogStrategy.TAG_ERROR_KIND, it.javaClass.simpleName)
jsonLog.addProperty(LogStrategy.TAG_ERROR_MESSAGE, it.message)
jsonLog.addProperty(LogStrategy.TAG_ERROR_STACK, sw.toString())
}

return jsonLog.toString()
}

private fun obfuscateAndWriteLog(strLog: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ internal class DatadogTest {
}
}

fun `it will initialize all dependencies at initialize`(){
fun `it will initialize all dependencies at initialize`() {
Datadog.initialize(mockContext, fakeToken)

assertThat(Datadog.packageName).isEqualTo(packageName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ import com.nhaarman.mockitokotlin2.whenever
import fr.xgouchet.elmyr.Forge
import fr.xgouchet.elmyr.annotation.Forgery
import fr.xgouchet.elmyr.junit5.ForgeExtension
import java.io.File
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.api.extension.Extensions
import org.junit.jupiter.api.io.TempDir
import org.mockito.Mock
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.junit.jupiter.MockitoSettings
Expand All @@ -42,15 +40,16 @@ internal class LoggerBuilderTest {
@Mock
lateinit var mockContext: Context

@TempDir
lateinit var tempDir: File
lateinit var packageName: String

@BeforeEach
fun `set up Datadog`(forge: Forge) {
packageName = forge.anAlphabeticalString()
val mockContext: Context = mock()
val mockConnectivityMgr: ConnectivityManager = mock()
val mockNetworkInfo: NetworkInfo = mock()
whenever(mockContext.applicationContext) doReturn mockContext
whenever(mockContext.packageName) doReturn packageName
whenever(mockContext.getSystemService(Context.CONNECTIVITY_SERVICE))
.doReturn(mockConnectivityMgr)
whenever(mockConnectivityMgr.activeNetworkInfo) doReturn mockNetworkInfo
Expand All @@ -73,6 +72,7 @@ internal class LoggerBuilderTest {
assertThat(logger.datadogLogsEnabled).isTrue()
assertThat(logger.logcatLogsEnabled).isFalse()
assertThat(logger.networkInfoProvider).isNull()
assertThat(logger.loggerName).isEqualTo(packageName)
}

@Test
Expand Down Expand Up @@ -123,4 +123,15 @@ internal class LoggerBuilderTest {
assertThat(logger.networkInfoProvider).isNull()
}
}

@Test
fun `buider can set the logger name`(@Forgery forge: Forge) {
val loggerName = forge.anAlphabeticalString()

val logger = Logger.Builder()
.setLoggerName(loggerName)
.build()

assertThat(logger.loggerName).isEqualTo(loggerName)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ internal class LoggerFullFeaturesTest {
lateinit var fakeMessage: String
lateinit var fakeUserAgent: String
lateinit var fakeNetworkInfo: NetworkInfo
lateinit var fakeLoggerName: String

@Mock
lateinit var mockContext: Context
Expand All @@ -73,13 +74,15 @@ internal class LoggerFullFeaturesTest {
fakeMessage = forge.anAlphabeticalString()
fakeUserAgent = forge.anAlphabeticalString()
fakeNetworkInfo = forge.getForgery()
fakeLoggerName = forge.anAlphabeticalString()
whenever(mockNetworkInfoProvider.getLatestNetworkInfos()) doReturn fakeNetworkInfo

testedLogger = Logger.Builder()
.setServiceName(fakeServiceName)
.setLogcatLogsEnabled(true)
.setDatadogLogsEnabled(true)
.setNetworkInfoEnabled(true)
.setLoggerName(fakeLoggerName)
.withLogStrategy(mockLogStrategy)
.withNetworkInfoProvider(mockNetworkInfoProvider)
.build()
Expand Down Expand Up @@ -167,6 +170,7 @@ internal class LoggerFullFeaturesTest {
.hasMessage(fakeMessage)
.hasTimestamp(timestamp)
.hasNetworkInfo(fakeNetworkInfo)
.hasLoggerName(fakeLoggerName)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,20 @@ internal class LogAssert(actual: Log) :
assertThat(actual.networkInfo)
.overridingErrorMessage(
"Expected log to have networkInfo $expected " +
"but was ${actual.networkInfo}"
"but was ${actual.networkInfo}"
)
.isEqualTo(expected)
return this
}

fun hasLoggerName(expected: String) {
assertThat(actual.loggerName)
.overridingErrorMessage(
"Expected log to have loggerName $expected but was ${actual.loggerName}"
)
.isEqualTo(expected)
}

companion object {

private const val TIMESTAMP_THRESHOLD_MS = 50
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ internal class LogForgeryFactory : ForgeryFactory<Log> {
throwable = forge.getForgery(),
attributes = forge.exhaustiveAttributes(),
tags = forge.exhaustiveTags(),
networkInfo = forge.getForgery()
networkInfo = forge.getForgery(),
loggerName = forge.anAlphabeticalString(size = forge.aTinyInt())
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ internal abstract class LogStrategyTest {
carrierName = forge.anAlphabeticalString(size = 256)
),
throwable = ArrayIndexOutOfBoundsException(forge.anAlphabeticalString()),
timestamp = forge.aLong()
timestamp = forge.aLong(),
loggerName = forge.anAlphabeticalString()
)

testedLogWriter.writeLog(bigLog)
Expand Down Expand Up @@ -409,9 +410,16 @@ internal abstract class LogStrategyTest {
throwable.printStackTrace(PrintWriter(sw))

assertThat(jsonObject)
.hasField(LogStrategy.TAG_LOGGER_NAME, log.loggerName)
.hasField(LogStrategy.TAG_ERROR_KIND, throwable.javaClass.simpleName)
.hasField(LogStrategy.TAG_ERROR_MESSAGE, throwable.message)
.hasField(LogStrategy.TAG_ERROR_STACK, sw.toString())
} else {
assertThat(jsonObject)
.doesNotHaveField(LogStrategy.TAG_LOGGER_NAME)
.doesNotHaveField(LogStrategy.TAG_ERROR_KIND)
.doesNotHaveField(LogStrategy.TAG_ERROR_MESSAGE)
.doesNotHaveField(LogStrategy.TAG_ERROR_STACK)
}
}

Expand Down

0 comments on commit e5bf9bf

Please sign in to comment.