forked from oshai/kotlin-logging
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow supplying org.slf4j.Marker to the logger
this fixes oshai#360
- Loading branch information
Showing
3 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/javaMain/kotlin/io/github/oshai/kotlinlogging/slf4j/internal/Slf4jMarker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.github.oshai.kotlinlogging.slf4j.internal | ||
|
||
import io.github.oshai.kotlinlogging.Marker | ||
|
||
internal class Slf4jMarker(val marker: org.slf4j.Marker) : Marker, org.slf4j.Marker by marker |
75 changes: 75 additions & 0 deletions
75
src/jvmTest/kotlin/io/github/oshai/kotlinlogging/slf4j/Slf4jMarkerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package io.github.oshai.kotlinlogging.slf4j | ||
|
||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import io.github.oshai.kotlinlogging.addAppender | ||
import io.github.oshai.kotlinlogging.removeAppender | ||
import kotlin.test.assertEquals | ||
import org.apache.logging.log4j.core.Appender | ||
import org.apache.logging.log4j.core.LogEvent | ||
import org.apache.logging.log4j.core.appender.NullAppender | ||
import org.apache.logging.slf4j.Log4jMarker | ||
import org.junit.jupiter.api.AfterEach | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
|
||
private val logger = KotlinLogging.logger {} | ||
|
||
private class TestAppender( | ||
val appender: Appender = NullAppender.createAppender("testAppender"), | ||
var lastEvent: LogEvent? = null, | ||
) : Appender by appender { | ||
override fun append(event: LogEvent) { | ||
lastEvent = event.toImmutable() | ||
appender.append(event) | ||
} | ||
} | ||
|
||
class Slf4jMarkerTest { | ||
|
||
private val testAppender = TestAppender() | ||
|
||
@BeforeEach | ||
fun setupAppender() { | ||
addAppender(testAppender) | ||
} | ||
|
||
@AfterEach | ||
fun removeAppender() { | ||
removeAppender(testAppender) | ||
} | ||
|
||
@Test | ||
fun `a slf4j Marker can be supplied to the logger`() { | ||
|
||
val log4jMarker = | ||
object : org.apache.logging.log4j.Marker { | ||
override fun addParents( | ||
vararg markers: org.apache.logging.log4j.Marker? | ||
): org.apache.logging.log4j.Marker = TODO("Not yet implemented") | ||
|
||
override fun getName(): String = "foo" | ||
|
||
override fun getParents(): Array<org.apache.logging.log4j.Marker> = | ||
TODO("Not yet implemented") | ||
|
||
override fun hasParents(): Boolean = false | ||
|
||
override fun isInstanceOf(m: org.apache.logging.log4j.Marker?): Boolean = false | ||
|
||
override fun isInstanceOf(name: String?): Boolean = false | ||
|
||
override fun remove(marker: org.apache.logging.log4j.Marker?): Boolean = | ||
TODO("Not yet implemented") | ||
|
||
override fun setParents( | ||
vararg markers: org.apache.logging.log4j.Marker? | ||
): org.apache.logging.log4j.Marker = TODO("Not yet implemented") | ||
} | ||
|
||
val slf4jMarker = Log4jMarker(null, log4jMarker) | ||
|
||
logger.atError(slf4jMarker.toKotlinLogging()) { message = "bar" } | ||
|
||
assertEquals(testAppender.lastEvent?.marker, log4jMarker) | ||
} | ||
} |