Using log4j2 context seems a bit limited for non-thread use cases. The assumption that context will always be set at the Thread or coroutine level is IMO false, and sometimes we just want to set context for a particular block of code, or for a particular logger.
We should have a way to do something like this outside of a coroutine / suspending context:
withContextMap(...) {
// log something
}
Possible implementations (only map shown here, but similar for stack)?
fun <T> withContextMap(
map: Map<String, String>,
block: () -> T,
): T {
val oldMap = ContextMap.view
return try {
ContextMap += map
block()
} finally {
ContextMap.clear()
ContextMap += oldMap
}
}
Could also be related to #36. We may want to do something like:
val log = parentLog.withContext(...)
...
log.debug { ... }