From 1d4022453933e8436c88519d69897f07d6128688 Mon Sep 17 00:00:00 2001 From: Morten Byhring Date: Mon, 21 Aug 2023 10:56:40 +0200 Subject: [PATCH] =?UTF-8?q?tillat=20nullable=20verdier,=20gj=C3=B8r=20det?= =?UTF-8?q?=20enklere=20for=20applikasjoner=20=C3=A5=20logge,=20uten=20?= =?UTF-8?q?=C3=A5=20vite=20hvilke=20uuid-er=20som=20er=20satt=20/=20tilgje?= =?UTF-8?q?ngelig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 2 +- .../nav/helsearbeidsgiver/utils/log/MdcUtils.kt | 2 +- .../helsearbeidsgiver/utils/log/MdcUtilsTest.kt | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index f7ef40d..6045177 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,7 +1,7 @@ import org.jetbrains.kotlin.gradle.dsl.JvmTarget group = "no.nav.helsearbeidsgiver" -version = "0.5.5" +version = "0.5.6" plugins { kotlin("jvm") diff --git a/src/main/kotlin/no/nav/helsearbeidsgiver/utils/log/MdcUtils.kt b/src/main/kotlin/no/nav/helsearbeidsgiver/utils/log/MdcUtils.kt index e9d5920..6e30e64 100644 --- a/src/main/kotlin/no/nav/helsearbeidsgiver/utils/log/MdcUtils.kt +++ b/src/main/kotlin/no/nav/helsearbeidsgiver/utils/log/MdcUtils.kt @@ -29,7 +29,7 @@ object MdcUtils { block = block ) - inline fun withLogFields(vararg logFields: Pair, block: () -> T): T { + inline fun withLogFields(vararg logFields: Pair, block: () -> T): T { val backup = logFields.map { (key, _) -> key to MDC.get(key) } diff --git a/src/test/kotlin/no/nav/helsearbeidsgiver/utils/log/MdcUtilsTest.kt b/src/test/kotlin/no/nav/helsearbeidsgiver/utils/log/MdcUtilsTest.kt index 3a827f8..2acc5a2 100644 --- a/src/test/kotlin/no/nav/helsearbeidsgiver/utils/log/MdcUtilsTest.kt +++ b/src/test/kotlin/no/nav/helsearbeidsgiver/utils/log/MdcUtilsTest.kt @@ -173,6 +173,22 @@ class MdcUtilsTest : FunSpec({ MDC.get("black").shouldBeNull() } + + test("logger ikke null-verdier") { + val fnWithNonLocalReturn = fun(): String = + MdcUtils.withLogFields( + "empty" to null, + "non-empty" to "not null" + ) { + MDC.get("empty") shouldBe null + MDC.get("non-empty") shouldBe "not null" + } + + fnWithNonLocalReturn() + + MDC.get("empty").shouldBeNull() + MDC.get("non-empty").shouldBeNull() + } } })