kotlin-logging
kotlin-logging copied to clipboard
Add `withLoggingContextAsync`
For coroutines to propagate MDC context properly, we need to write code as
withLoggingContext("foo" to bar, "xyz" to thing) {
withContext(MDCContext()) {
// suspending block
}
}
It'd be a lot more convenient if we could add a new method that took a suspending method as a default:
public inline fun <T> withLoggingContextAsync(vararg pair: Pair<String, String>, body: suspend () -> T): T
and reduced this boilerplate.
Happy to submit a PR if we agree on the nomenclature
Thanks! A PR is welcome, but the reason it wasn't done in the first place is that I think it will require an additional dependency on org.jetbrains.kotlinx:kotlinx-coroutines-slf4j
. We should think of a way to avoid that for cases not using coroutines.
If there's an existing slf4j dependency, would it be too problematic to introduce a kotlinx-coroutines-slf4j
dependency?
Yes, because that adds a dependency on kotlinx-coroutines
as well for anyone using kotlin-logging regardless if coroutines are used.. I prefer to keep kotlin-logging
light and without that dependency consider the fact that we are talking about one method. I think a better approach might be to add such method to kotlinx-coroutines-slf4j
itself or a (new) integration module kotlinx-coroutines-kotlin-logging
?
I tried taking this up but got blocked on some dependency version resolution issue in the Kotlinx Coroutines library; I'll post an update if/when I get one.
PR in the Kolinx Coroutines repository is up, if you'd like to take a look at it.
...and it got rejected. @oshai looks like you'll need to create another library to contain this integration, because the Coroutine library maintainers don't want it in their code base.
@severn-everett another option is that you'll maintain such module in another repo if you'd like.
Probably a better way to put is whether you know how they create the integration modules in the Kotlinx Coroutines library, as I could put the code there. It could be published as something like kotlin-logging-jvm-coroutine
so that developers could use withLoggingContextAsync
without requiring the Coroutines library as a dependency for the base kotlin-logging
jar. If you don't know, I could ask the Coroutines team how they do it.
Yes, if you know how to do that I'm ok with such PR.
So the current status is: PR for integration module was rejected by kotlin team as they don't want to support another module: https://github.com/Kotlin/kotlinx.coroutines/pull/3092
One option to try is to have the existing slf4j module with kotlin-logging dependency.
Another option is to have an additional module here (will require changes to the lib structure).
I created an initial PR in the same module, the only caveat is that users will have to add the slf4j coroutines dep is it is provided only.
https://github.com/oshai/kotlin-logging/pull/278
Added in 4.0.0-beta-20.