Skip to content

Commit

Permalink
Add mdc() DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
leadrian committed Aug 30, 2023
1 parent 6b1068d commit 7382d01
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ dependencies {
testImplementation(libs.mockk)

testRuntimeOnly(libs.junit.jupiter.engine)
testRuntimeOnly(libs.slf4j.simple)
testRuntimeOnly(libs.logback)
}

val gitVersion: Closure<String> by extra
Expand All @@ -51,7 +51,7 @@ version = gitVersion()

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(8))
languageVersion.set(JavaLanguageVersion.of(11))
}

withSourcesJar()
Expand Down
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
dokka = "1.8.20"
git-version = "3.0.0"
junit = "5.10.0"
logback = "1.4.8"
mockk = "1.13.5"
slf4j = "2.0.7"

[libraries]
junit-jupiter-api = { group = "org.junit.jupiter", name = "junit-jupiter-api", version.ref = "junit" }
junit-jupiter-engine = { group = "org.junit.jupiter", name = "junit-jupiter-engine", version.ref = "junit" }
logback = { group = "ch.qos.logback", name = "logback-classic", version.ref = "logback" }
mockk = { group = "io.mockk", name = "mockk", version.ref = "mockk" }
slf4j-api = { group = "org.slf4j", name = "slf4j-api", version.ref = "slf4j" }
slf4j-simple = { group = "org.slf4j", name = "slf4j-simple", version.ref = "slf4j" }

[plugins]
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
Expand Down
31 changes: 31 additions & 0 deletions src/main/kotlin/ch/leadrian/slf4k/Slf4k.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package ch.leadrian.slf4k

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.slf4j.MDC
import org.slf4j.Marker

/**
Expand Down Expand Up @@ -220,4 +221,34 @@ inline fun Logger.error(marker: Marker, throwable: Throwable?, message: () -> St
if (isErrorEnabled) {
error(marker, message(), throwable)
}
}

/**
* Execute the given [action] in an MDC context where the [key] is assigned [value].
*
* @return the result of [action], may be [Unit]
*/
inline fun <T> mdc(key: String, value: String?, action: () -> T): T {
putCloseable(key, value).use {
return action()
}
}

@PublishedApi
internal fun putCloseable(key: String, value: String?): AutoCloseable {
val previousValue = MDC.get(key)
return if (previousValue != null) {
val closeable = RestoringMDCCloseable(key, previousValue)
MDC.put(key, value)
closeable
} else {
MDC.putCloseable(key, value)
}
}

private class RestoringMDCCloseable(private val key: String, private val previousValue: String) : AutoCloseable {

override fun close() {
MDC.put(key, previousValue)
}
}
18 changes: 18 additions & 0 deletions src/test/kotlin/ch/leadrian/slf4k/Slf4kTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ import io.mockk.just
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertAll
import org.slf4j.Logger
import org.slf4j.MDC
import org.slf4j.Marker

internal class Slf4kTest {
Expand Down Expand Up @@ -538,5 +541,20 @@ internal class Slf4kTest {

assertEquals("TestLogger", logger.name)
}

@Test
fun `should add MDC context`() {
val valueBefore = MDC.get("foo")
val valueDuring = mdc("foo", "bar") {
MDC.get("foo")
}
val valueAfter = MDC.get("foo")

assertAll(
{ assertNull(valueBefore) },
{ assertEquals("bar", valueDuring) },
{ assertNull(valueAfter) },
)
}
}

0 comments on commit 7382d01

Please sign in to comment.