Skip to content

Commit

Permalink
fix up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Sep 29, 2024
1 parent 2a06c2d commit 27311fa
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
14 changes: 13 additions & 1 deletion core/src/main/kotlin/logging/Logger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package logging
import ch.qos.logback.classic.Level
import ch.qos.logback.classic.spi.ILoggingEvent
import getPercentage
import java.lang.reflect.Field
import java.util.concurrent.ConcurrentHashMap
import kotlin.math.floor
import net.logstash.logback.marker.MapEntriesAppendingMarker
import net.logstash.logback.marker.Markers
import org.slf4j.Marker

Expand Down Expand Up @@ -85,6 +87,16 @@ val ILoggingEvent.loggingCode: String?

fun ILoggingEvent.findLogField(key: String): Any? = getLogFields()[key]

fun ILoggingEvent.getLogFields() = this.keyValuePairs.associate { it.key to it.value }
private val mapEntriesAppendingMarkerField: Field =
MapEntriesAppendingMarker::class.java.getDeclaredField("map").apply { isAccessible = true }

fun ILoggingEvent.getLogFields(): Map<String, Any> {
val marker = this.markerList?.firstOrNull()
return if (marker is MapEntriesAppendingMarker) {
mapEntriesAppendingMarkerField.get(marker) as Map<String, Any>
} else {
emptyMap()
}
}

fun ILoggingEvent.level() = this.throwableProxy
7 changes: 4 additions & 3 deletions core/src/test/kotlin/logging/LoggerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class LoggerTest : AbstractTest() {
record.loggingCode shouldBe loggingCode
record.message shouldBe "A thing happened"
record.throwableProxy shouldBe null
record.getLogFields().size shouldBe 0
record.shouldContainKeyValues("loggingCode" to "some.event")
}

@Test
Expand Down Expand Up @@ -51,7 +51,6 @@ class LoggerTest : AbstractTest() {
record.loggingCode shouldBe loggingCode
record.message shouldBe "A slightly bad thing happened"
record.throwableProxy shouldBe null
record.keyValuePairs.size shouldBe 0
}

@Test
Expand All @@ -64,9 +63,11 @@ class LoggerTest : AbstractTest() {

val record = getLastLog()
record.level shouldBe Level.ERROR
record.throwableProxy shouldBe throwable
record.throwableProxy.message shouldBe "Boo"
record.loggingCode shouldBe loggingCode
record.shouldContainKeyValues("other.info" to 60, KEY_EXCEPTION_MESSAGE to "Boo")

errorLogged() shouldBe true
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logging

import ch.qos.logback.classic.Level
import io.kotest.matchers.maps.shouldNotContainKey
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import main.kotlin.testCore.shouldContainKeyValues
Expand Down Expand Up @@ -33,11 +34,9 @@ class LoggerUncaughtExceptionHandlerTest : AbstractTest() {
handler.uncaughtException(Thread.currentThread(), ex)

val log = verifyLog("uncaughtException", Level.ERROR)
log.throwableProxy shouldBe ex
log.shouldContainKeyValues(
KEY_THREAD to Thread.currentThread().toString(),
KEY_EXCEPTION_MESSAGE to null
)
log.throwableProxy.message shouldBe null
log.shouldContainKeyValues(KEY_THREAD to Thread.currentThread().toString())
log.getLogFields().shouldNotContainKey(KEY_EXCEPTION_MESSAGE)
log.message shouldContain "Uncaught exception: $ex"
}

Expand All @@ -50,7 +49,7 @@ class LoggerUncaughtExceptionHandlerTest : AbstractTest() {
handler.uncaughtException(t, ex)

val log = verifyLog("uncaughtException", Level.ERROR)
log.throwableProxy shouldBe ex
log.throwableProxy.message shouldBe "Argh"
log.shouldContainKeyValues(KEY_THREAD to t.toString(), KEY_EXCEPTION_MESSAGE to "Argh")
log.message shouldContain "Uncaught exception: $ex"
}
Expand Down
4 changes: 2 additions & 2 deletions test-core/src/main/kotlin/testCore/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ch.qos.logback.classic.spi.ILoggingEvent
import com.github.alyssaburlton.swingtest.findAll
import com.github.alyssaburlton.swingtest.findWindow
import com.github.alyssaburlton.swingtest.flushEdt
import io.kotest.matchers.maps.shouldContainExactly
import io.kotest.matchers.maps.shouldContainAll
import io.kotest.matchers.shouldBe
import io.mockk.verify
import java.time.Instant
Expand All @@ -16,7 +16,7 @@ import logging.LogRecord
import logging.getLogFields

fun ILoggingEvent.shouldContainKeyValues(vararg values: Pair<String, Any?>) {
this.getLogFields().shouldContainExactly(mapOf(*values))
this.getLogFields().shouldContainAll(mapOf(*values))
}

fun makeLogRecord(
Expand Down

0 comments on commit 27311fa

Please sign in to comment.