Skip to content

Commit

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ private constructor(
attributes = attributes.toMap(),
tags = tags.toList(),
networkInfo = networkInfoProvider?.getLatestNetworkInfos(),
loggerName = loggerName
loggerName = loggerName,
threadName = Thread.currentThread().name
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ internal data class Log(
val tags: List<String>,
val throwable: Throwable?,
val networkInfo: NetworkInfo?,
val loggerName: String
val loggerName: String,
val threadName: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ internal interface LogStrategy {

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

// ANDROID SPECIFIC TAGS
internal const val TAG_NETWORK = "network"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ internal class LogFileWriter(
) {
log.throwable?.let {
jsonLog.addProperty(LogStrategy.TAG_LOGGER_NAME, log.loggerName)
jsonLog.addProperty(LogStrategy.TAG_THREAD_NAME, log.threadName)
val sw = StringWriter()
it.printStackTrace(PrintWriter(sw))
jsonLog.addProperty(LogStrategy.TAG_ERROR_KIND, it.javaClass.simpleName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import fr.xgouchet.elmyr.junit5.ForgeConfiguration
import fr.xgouchet.elmyr.junit5.ForgeExtension
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.util.concurrent.CountDownLatch
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
Expand Down Expand Up @@ -77,15 +78,7 @@ internal class LoggerFullFeaturesTest {
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()
testedLogger = createLogger()
}

@BeforeEach
Expand Down Expand Up @@ -150,10 +143,36 @@ internal class LoggerFullFeaturesTest {
verifyLogSideEffects(AndroidLog.ASSERT, "A")
}

@Test
fun `the thread name will be the name of the thread from which the logger was built`() {
val countDownLatch = CountDownLatch(1)
var logger: Logger
Thread().start().run {
logger = createLogger()
countDownLatch.countDown()
}
countDownLatch.await()

logger.e(fakeMessage)
verifyLogSideEffects(AndroidLog.ERROR, "E")
}

// endregion

// region Internal

private fun createLogger(): Logger {
return Logger.Builder()
.setServiceName(fakeServiceName)
.setLogcatLogsEnabled(true)
.setDatadogLogsEnabled(true)
.setNetworkInfoEnabled(true)
.setLoggerName(fakeLoggerName)
.withLogStrategy(mockLogStrategy)
.withNetworkInfoProvider(mockNetworkInfoProvider)
.build()
}

private fun verifyLogSideEffects(level: Int, logCatPrefix: String) {
val timestamp = System.currentTimeMillis()

Expand All @@ -171,6 +190,7 @@ internal class LoggerFullFeaturesTest {
.hasTimestamp(timestamp)
.hasNetworkInfo(fakeNetworkInfo)
.hasLoggerName(fakeLoggerName)
.hasThreadName(Thread.currentThread().name)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,22 @@ internal class LogAssert(actual: Log) :
return this
}

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

fun hasThreadName(expected: String): LogAssert {
assertThat(actual.threadName)
.overridingErrorMessage(
"Expected log to have threadName $expected but was ${actual.threadName}"
)
.isEqualTo(expected)
return this
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ internal class LogForgeryFactory : ForgeryFactory<Log> {
attributes = forge.exhaustiveAttributes(),
tags = forge.exhaustiveTags(),
networkInfo = forge.getForgery(),
loggerName = forge.anAlphabeticalString(size = forge.aTinyInt())
loggerName = forge.anAlphabeticalString(),
threadName = forge.anAlphabeticalString()
)
}

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

testedLogWriter.writeLog(bigLog)
Expand Down Expand Up @@ -411,12 +412,14 @@ internal abstract class LogStrategyTest {

assertThat(jsonObject)
.hasField(LogStrategy.TAG_LOGGER_NAME, log.loggerName)
.hasField(LogStrategy.TAG_THREAD_NAME, log.threadName)
.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_THREAD_NAME)
.doesNotHaveField(LogStrategy.TAG_ERROR_KIND)
.doesNotHaveField(LogStrategy.TAG_ERROR_MESSAGE)
.doesNotHaveField(LogStrategy.TAG_ERROR_STACK)
Expand Down

0 comments on commit 860ebbb

Please sign in to comment.