Skip to content

Commit aa5b47c

Browse files
authored
rebase (#33)
* issue #31 - Add MDC support: withLoggingContext * Update ChangleLog.md * Update ChangleLog.md * Update README.md * Rename ChangleLog.md to ChangeLog.md (#32)
1 parent c6dc3a8 commit aa5b47c

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

ChangleLog.md renamed to ChangeLog.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.4.9
2+
* Add MDC Support [#31](https://github.com/MicroUtils/kotlin-logging/issues/31)
3+
* Upgrade to Kotlin 1.2.10.
4+
15
# 1.4.8
26
* 1.4.7 was broken see issue [#30](https://github.com/MicroUtils/kotlin-logging/issues/30)
37

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ An `Android` example project with kotlin logging can be found in [kotlin-logging
5353
<dependency>
5454
<groupId>io.github.microutils</groupId>
5555
<artifactId>kotlin-logging</artifactId>
56-
<version>1.4.7</version>
56+
<version>1.4.9</version>
5757
</dependency>
5858
```
5959
See full example in [kotlin-logging-example-maven](https://github.com/MicroUtils/kotlin-logging-example-maven).
6060

6161
### Gradle
6262
```Groovy
63-
compile 'io.github.microutils:kotlin-logging:1.4.7'
63+
compile 'io.github.microutils:kotlin-logging:1.4.9'
6464
```
6565

6666
Or alternatively, download jar from [github](https://github.com/MicroUtils/kotlin-logging/releases/latest) or [bintray](https://dl.bintray.com/microutils/kotlin-logging/io/github/microutils/kotlin-logging/) or [maven-central](http://repo1.maven.org/maven2/io/github/microutils/kotlin-logging/).
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package mu
2+
3+
import org.slf4j.MDC
4+
5+
inline fun <T> withLoggingContext(pair: Pair<String, String>, body: () -> T): T =
6+
MDC.putCloseable(pair.first, pair.second).use { body() }
7+
8+
inline fun <T> withLoggingContext(vararg pair: Pair<String, String>, body: () -> T): T {
9+
try {
10+
pair.forEach { MDC.put(it.first, it.second) }
11+
return body()
12+
} finally {
13+
pair.forEach { MDC.remove(it.first) }
14+
}
15+
}
16+
17+
inline fun <T> withLoggingContext(map: Map<String, String>, body: () -> T): T {
18+
try {
19+
map.forEach { MDC.put(it.key, it.value) }
20+
return body()
21+
} finally {
22+
map.forEach { MDC.remove(it.key) }
23+
}
24+
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package mu
2+
3+
import org.junit.Assert.*
4+
import org.junit.Test
5+
import org.slf4j.MDC
6+
7+
class KotlinLoggingMDCTest {
8+
9+
@Test
10+
fun `simple pair withLoggingContext`() {
11+
assertNull(MDC.get("a"))
12+
withLoggingContext("a" to "b") {
13+
assertEquals("b", MDC.get("a"))
14+
}
15+
assertNull(MDC.get("a"))
16+
}
17+
18+
@Test
19+
fun `multiple pair withLoggingContext`() {
20+
assertNull(MDC.get("a"))
21+
assertNull(MDC.get("c"))
22+
withLoggingContext("a" to "b", "c" to "d") {
23+
assertEquals("b", MDC.get("a"))
24+
assertEquals("d", MDC.get("c"))
25+
}
26+
assertNull(MDC.get("a"))
27+
assertNull(MDC.get("c"))
28+
}
29+
30+
@Test
31+
fun `map withLoggingContext`() {
32+
assertNull(MDC.get("a"))
33+
assertNull(MDC.get("c"))
34+
withLoggingContext(mapOf("a" to "b", "c" to "d")) {
35+
assertEquals("b", MDC.get("a"))
36+
assertEquals("d", MDC.get("c"))
37+
}
38+
assertNull(MDC.get("a"))
39+
assertNull(MDC.get("c"))
40+
}
41+
}

0 commit comments

Comments
 (0)