John Kasper Svergja
10/17/2022, 11:17 AMwithLoggingContext
). Is there considered any best practices for MDC and exceptions?
The current implementation of withLoggingContext clears all MDC fields if an exception inside the flow occurs. I would very much like the exception to contain the MDC-information.
One solution could be that withLoggingContext
provides an option to provided an exception handler. (I think it is important not to couple the exception handling to close to the framework).
Do you have any thoughts about this/what do you think? Or any suggestion on how the mdc is kept for exceptions?
For my case I think the best use of an exception handler would be to add an “meta”-exception to the original exception (addSuppressed
method ) (which is what I have done in my code, by creating a wrapper method around withLoggingContext
)Endre Deak
11/16/2022, 9:44 PMimport kotlinx.coroutines.runBlocking
import mu.KotlinLogging
import org.junit.jupiter.api.Test
internal class MdcTest {
@Test
fun testMdcContextBehavior() = runBlocking {
withContext(MDCContext(mapOf("foo" to "bar"))) {
logger.warn { "context should have bar" }
assertEquals(MDC.get("foo"}), "bar")
awaitAll(
async { assertMdcContext("bar1") },
async { assertMdcContext("bar2") }
)
logger.warn { "context should have bar " }
assertEquals(MDC.get("foo"), "bar")
}
}
private suspend fun assertMdcContext(id: String) {
withContext(MDCContext(mapOf("foo" to id))) {
logger.warn { "message with $id" }
assertEquals(MDC.get("foo"), id)
}
}
}
oshai
12/18/2022, 2:33 PMDjuro
05/26/2023, 8:08 PMoshai
05/29/2023, 6:12 PMoshai
06/17/2023, 9:18 AMio.github.oshai:kotlin-logging-jvm:4.0.0-beta-29
benkuly
06/29/2023, 3:08 PMcom.github.tony19:logback-android
with version 4?oshai
07/13/2023, 7:48 AM윤동환
07/25/2023, 1:28 PMjavaru
08/02/2023, 8:11 PMKLogger
into some methods requiring a SLF4J Logger
. In 5.x, Klogger
no longer extends org.slf4j.Logger
And so we can’t directly pass it in. In this comment for Issue 264, it was said:
The idea is that slf4j will still be supported, but instead of inheritance it will be via wrapping it.I’m not seeing how to wrap a
KLogger
in order to make it useable as a SLF4J Logger
. Any guidance?Victor Harlan Lacson
08/04/2023, 7:49 AMholgerbrandl
08/09/2023, 9:19 AMinternal
to allow visibility from other source modules in the same gradle project? E.g. I want to use some internal
method defined in the main module from within the test module.pambrose
08/23/2023, 6:50 PMcompanion object : KLogging()
It appears that with 5.0, one has to explicitly create a logger value with val logger = KotlinLogging.logger {}
. Is that correct?oshai
08/27/2023, 3:30 PMAsaf Peleg
10/03/2023, 1:07 AMMitul
10/31/2023, 12:49 PMMitul
10/31/2023, 12:49 PMPhil Richardson
11/22/2023, 10:25 AMMark
12/21/2023, 8:40 AMLocationAwareKLogger
actually grab the class name from which the log call is made? I can’t seem to find the relevant code.
For context of why I’m asking: https://kotlinlang.slack.com/archives/CTJB58X7X/p1703144409394349Klitos Kyriacou
01/09/2024, 3:40 PMMikolaj
02/22/2024, 10:15 AMAtul Gupta
08/18/2024, 5:47 PMkotlin-logger
vs Napier?Gilles Barbier
08/21/2024, 10:09 PMnull
? I do not want to do expr?.<http://logger.info|logger.info> { it }
to avoid calculating the expr whatever the logging level.Sabeeh
10/17/2024, 8:41 AMChris Lee
10/31/2024, 12:15 AMlaunch(MDCcontext())
all over the place or things break.Nikky
10/31/2024, 5:28 AMDmytro Koval
11/06/2024, 11:58 AMSlackbot
01/16/2025, 6:37 PMAbhilash Mandaliya
07/18/2025, 6:23 AMpublic fun info(msg: String?, vararg arguments: Any?): Unit
are deprecated with a suggestion to
ReplaceWith("info { \"\$msg \$arguments\"}")
However, this can't honor formatting which was applied by previous underlying slf4j Logger e.g.
log.info("hello {}", "world")
How to deal with this? Our project is relying on this format at 100s of places.
Instead of deprecating those methods, could we have made them open and default implementation could have been what we suggest in the ReplaceWith. So that user can write a wrapper on top of KLogger or delegate the implementation with some trick like below:
override fun trace(format: String, vararg args: Any) {
if (this is Slf4jLogger<*>) {
underlyingLogger.trace(format, *args)
} else {
super.trace(format, *args)
}
}
Appreciate the response from community 🙏oshai
07/20/2025, 7:31 PM