Comments (11)
ok, I will implement few flavors, let me know what you think.
from kotlin-logging.
coroutines support is now available, more details here: https://github.com/MicroUtils/kotlin-logging/wiki#mdc-and-threadscoroutines
from kotlin-logging.
looks like a neat idea! any other suggestions?
from kotlin-logging.
How about an extension method instead:
inline fun <T> Pair<String, String>.asLoggingContextOf(body: () -> T): T =
MDC.putCloseable(first, second).use { body() }
from kotlin-logging.
That works well if you have a single key/value pair you want to put into context.
How would multiple pairs or "additive" contexts look like?
I think that coming up with the right API here will be a bit difficult, so it might be worth it to enumerate use cases. Here are at least a few I am thinking of:
-
wrapping some method body with single pair:
fun myFunction(name: String) { mdc.withContext("name" to name) { doThing() } }
-
wrapping multiple pairs
fun myFunction(name: String) { mdc.withContext("name" to name, "otherThing" to "otherValue") { doThing() } }
-
a map of parameters
fun myFunction(params: Map<String, String>) { mdc.withContext(params) { doThing() } }
-
coroutines support in (not sure how this works without more coroutine support, possibly in a different libarary or out of scope for this request)
fun myFunction(name: String) { launch(CommonPool) { mdc.withContext("name" to name) { // support for suspending functions? val thing1 = doThing1() val thing2 = doThing2() thing + thing2 } } }
from kotlin-logging.
I think coroutines are out of the initial scope as MDC current infra relies on threads.
Basically, for multiple pairs it is possible to do something like:
(key1 to value1).asLoggingContextOf {
(key2 to value2).asLoggingContextOf {
...
}
}
So the real question here is how required / strong is the use case. Currently, I don't use MDC (although I might start using it when support will be available ;-)
But I am not sure how strong is the use case. If it was I guess it would probably be implemented in MDC.putCloseable()
as well.
It is always easier to start with partial API and add later.
What do you think?
from kotlin-logging.
I agree in cutting MDC support for coroutines the first iteration of this. There might be some way to handle it with ContinuationInterceptor
but that can be held off on as it isn't the main use case (for me at least) right now.
The background for how I am hoping to migrate to MDC in one part of our application:
- We have an old log4j format custom layout that converts messages to JSON
- loggers construct and pass in a special type type we have to the
logger.info(Object message)
(and other levels) that has a map of keys and values that are added to to the constructed JSON. This gets output and consumed downstream in log collection infrastructure - SLF4J API takes a
String message
, so we can't really keep passing in our custom object
I'd like to get us moving forward for us by:
- Switch all of our stuff from using Log4j directly to SLF4J
- Switch to using this library (which we are using in other places already)
- store those key/values in MDC and have a
Layout
pull them out and format the message as JSON instead
In our case, our key/value maps sometimes have 9-10 keys and values, so repeating (k to v).asLoggingContextOf {}
that many nested times is not desirable.
vararg Pair<>
(or N-arg Pair
methods similar to Guava with https://google.github.io/guava/releases/21.0/api/docs/com/google/common/collect/ImmutableList.html#of-E-E-E-E-E-E-E-E-E-E-E- and https://google.github.io/guava/releases/21.0/api/docs/com/google/common/collect/ImmutableList.html#of-E-E-E-E-E-E-E-E-E-E-E-E-E...-) or Map<>
methods would be more useful for our case.
We can do all of that migration ourselves, but Kotlin-friendly MDC
helpers are not present in this library (right now!). I can always try out a few different APIs in our own internal project and see how my teammates feel before following up here, but I thought other users may also look for it.
from kotlin-logging.
please see the commit let me know what you think, then I will push it to 1.4.9
from kotlin-logging.
👍 from me, looks good!
from kotlin-logging.
version 1.4.9 deployed.
from kotlin-logging.
More details on coroutines with mdc are here: Kotlin/kotlinx.coroutines#119
from kotlin-logging.
Related Issues (20)
- Payload not available to JS HOT 7
- underlyingLogger got a lot more complex HOT 5
- sflj jdk backend does not recognize class name HOT 14
- Gradle logging HOT 2
- Problems regarding Android HOT 3
- Problem using LogstashMarker HOT 15
- Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory HOT 2
- Declaring logger outside the class vs. inside HOT 8
- Possible to add a module-info.java for improved JPMS support? HOT 3
- setting logging level per KLogger instance in run-time HOT 4
- Built JAR Errors - SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" HOT 3
- Darwin doesn't log stacktraces HOT 2
- Publish Dokka HTML docs on project site HOT 2
- Why was KLogging deprecated? HOT 4
- KLoggingEventBuilder accept nullable values in payload HOT 4
- IllegalArgumentException when message includes {} HOT 5
- Android native targets missing. HOT 3
- Wrong class name printed when using LoggingEventBuilder with payload HOT 10
- Undo workaround for Gradle issue #11412 HOT 1
- Add timestamp to the KLoggingEvent HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kotlin-logging.