ryanbreaker
05/22/2025, 7:56 PMMDC.put(“key”, “val”)
to work within Ktor within a call pipeline? Is there a specific way to get this to work for the logger within Ktor? I’m using SLF4J with a JSON output and my goal is to simply add additional metadata to the JSON output based on the call.Jeff Hudson
05/22/2025, 7:57 PMorg.jetbrains.kotlinx:kotlinx-coroutines-slf4j:kotlinxCoroutines
we have a util
/**
* Use this to make MDC work properly with Kotlin coroutines.
* Null values will be excluded.
*/
public suspend fun <T> withMdc(mdc: Map<String, Any?>, block: suspend () -> T): T {
val contextMap = buildMap {
putAll(MDC.getCopyOfContextMap())
mdc.forEach { (key, value) ->
value ?: return@forEach // Don't include null values in MDC.
put(key, mdcMapper.writeValueAsStringSpecial(value))
}
}
return withContext(MDCContext(contextMap)) {
block()
}
}
Jeff Hudson
05/22/2025, 7:57 PMJeff Hudson
05/22/2025, 7:58 PMryanbreaker
05/22/2025, 8:07 PMryanbreaker
05/22/2025, 8:10 PMJeff Hudson
05/22/2025, 8:16 PMplugins {
id("com.google.cloud.artifactregistry.gradle-plugin")
}
maven {
url = uri("<artifactregistry://us-central1-maven.pkg.dev/airborne-software/maven>")
}
dependencies {
implementation("kairo:kairo-mdc:4.2.0")
}
Jeff Hudson
05/22/2025, 8:16 PMorg.jetbrains.kotlinx:kotlinx-coroutines-slf4j:kotlinxCoroutines
directly and just copy this fileJeff Hudson
05/22/2025, 8:17 PMJeff Hudson
05/22/2025, 8:19 PMryanbreaker
05/22/2025, 8:28 PMryanbreaker
05/22/2025, 8:38 PMryanbreaker
05/22/2025, 8:38 PMryanbreaker
05/22/2025, 8:40 PMMDC.put
calls, and then:
call.launch(MDCContext()) {
<http://application.log.info|application.log.info>(“…”)
}
ryanbreaker
05/22/2025, 8:40 PMAleksei Tirman [JB]
05/23/2025, 8:12 AMryanbreaker
05/23/2025, 2:07 PM