Hi all, new to Kotlin-logging…I was wondering if t...
# kotlin-logging
b
Hi all, new to Kotlin-logging…I was wondering if there’s support for a sort of “persistent” MDC, i.e. at a class scope. For example: I have a class which has an ID, and I want to log that id at the start of every log message written to that logger:
Copy code
class Foo(private val id: String) {
    private val logger = KotlinLogging.logger("...")
    
    fun bar() {
        <http://logger.info|logger.info> { "hello, world" } // want to be able to add a prefix so this logs something like: "[id: <the id>]: hello, world"
    }
}
b
I did find that page, but those all define context for function/coroutine type scopes, and I was wondering if there was a way to do a sort of “class” scope for context?
o
Mdc is used per flow or use case. Do you want something for each instance? You can have a logger per instance with different name though it's not the best practice
b
yeah, it may be that i’m going beyond the traditional “MDC” intention…but in the past i’ve added some custom logic to a logger that allowed for per-instance, inherited context that i found really useful, so i was wondering if i could leverage MDC in the same way.
o
Check slf4j docs. They have many features maybe something will fit
Note that mdc is kept in threadlocal
b
yeah, i think MDC is probably not what i’m after here…but any search for “log context” seems to turn up nothing but mdc-related stuff
d
Simple, unconventional, maybe a bit hacky solution but would do the job.
Copy code
fun KLogger.myInfo(id: Int, msg: () -> Any?) = info { "id: $id -- " + msg() }