kotlin-logging icon indicating copy to clipboard operation
kotlin-logging copied to clipboard

Add `withLoggingContextAsync`

Open tandel-pratik opened this issue 3 years ago • 9 comments

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

tandel-pratik avatar Feb 05 '21 18:02 tandel-pratik

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.

oshai avatar Feb 11 '21 22:02 oshai

If there's an existing slf4j dependency, would it be too problematic to introduce a kotlinx-coroutines-slf4j dependency?

tandel-pratik avatar Feb 11 '21 22:02 tandel-pratik

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?

oshai avatar Feb 12 '21 22:02 oshai

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.

severn-everett avatar Dec 15 '21 10:12 severn-everett

PR in the Kolinx Coroutines repository is up, if you'd like to take a look at it.

severn-everett avatar Dec 16 '21 09:12 severn-everett

...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 avatar Dec 16 '21 16:12 severn-everett

@severn-everett another option is that you'll maintain such module in another repo if you'd like.

oshai avatar Dec 16 '21 21:12 oshai

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.

severn-everett avatar Dec 18 '21 13:12 severn-everett

Yes, if you know how to do that I'm ok with such PR.

oshai avatar Dec 18 '21 20:12 oshai

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).

oshai avatar Feb 13 '23 22:02 oshai

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

oshai avatar Feb 17 '23 11:02 oshai

Added in 4.0.0-beta-20.

oshai avatar Feb 19 '23 22:02 oshai