apache / apache/logging-log4j-kotlin

Improve logging context integration

Open
#71 17 comments 0 reactions 0 assignees View on GitHub
Dominant language
Kotlin
Stars
53
Forks
14
PR merge metrics
No merged PRs in 30d

Description

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:

```kotlin
withContextMap(...) {
// log something
}
```

Possible implementations (only map shown here, but similar for stack)?

```kotlin
fun withContextMap(
map: Map,
block: () -> T,
): T {
val oldMap = ContextMap.view
return try {
ContextMap += map
block()
} finally {
ContextMap.clear()
ContextMap += oldMap
}
}
```

Could also be related to https://github.com/apache/logging-log4j-kotlin/issues/36. We may want to do something like:

```kotlin
val log = parentLog.withContext(...)

...

log.debug { ... }
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reviewing the existing Log4j2 context integration and the related issue 36, focusing on how context is handled outside thread or coroutine scopes. Define whether the completed work should provide block-scoped context, logger-specific context, or both, and verify that context is restored correctly after the block.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
observability-sre
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.