Skip to content

Commit e106815

Browse files
committed
Kotlin#119: first implementation of a SLF4J MDC Context
1 parent e0feeb9 commit e106815

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Module kotlinx-coroutines-slf4j
2+
3+
Integration with SLF4J [MDC](https://logback.qos.ch/manual/mdc.html).
4+
5+
## Example
6+
7+
Extends a given Coroutine context so that the SLF4J MDC context is passed into the coroutine.
8+
9+
```kotlin
10+
MDC.put("kotlin", "rocks") // put a value into the MDC context
11+
12+
launch(MDCContext(CommonPool)) {
13+
logger.info { "..." } // the MDC context will contain the mapping here
14+
}
15+
```
16+
17+
# Package kotlinx.coroutines.experimental.slf4j
18+
19+
Integration with SLF4J [MDC](https://logback.qos.ch/manual/mdc.html).

integration/kotlinx-coroutines-slf4j/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
dependencies {
22
compile 'org.slf4j:slf4j-api:1.7.25'
3+
testCompile 'io.github.microutils:kotlin-logging:1.5.4'
34
testRuntime 'ch.qos.logback:logback-classic:1.2.3'
45
testRuntime 'ch.qos.logback:logback-core:1.2.3'
56
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package kotlinx.coroutines.experimental.slf4j
2+
3+
import mu.KotlinLogging
4+
import org.slf4j.MDC
5+
6+
fun main(args: Array<String>) {
7+
val logger = KotlinLogging.logger {}
8+
9+
// You can put values in the MDC at any time. Before anything else
10+
// we put the first name
11+
MDC.put("first", "Dorothy")
12+
13+
// We now put the last name
14+
MDC.put("last", "Parker")
15+
16+
// The most beautiful two words in the English language according
17+
// to Dorothy Parker:
18+
logger.info("Check enclosed.")
19+
logger.debug("The most beautiful two words in English.")
20+
21+
MDC.put("first", "Richard")
22+
MDC.put("last", "Nixon")
23+
logger.info("I am not a crook.")
24+
logger.info("Attributed to the former US president. 17 Nov 1973.")
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
2+
<layout>
3+
<Pattern>%X{first} %X{last} - %m%n</Pattern>
4+
</layout>
5+
</appender>

0 commit comments

Comments
 (0)