Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow supplying org.slf4j.Marker #414

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.oshai.kotlinlogging.Level
import io.github.oshai.kotlinlogging.Marker
import io.github.oshai.kotlinlogging.slf4j.internal.Slf4jLoggerFactory
import io.github.oshai.kotlinlogging.slf4j.internal.Slf4jMarker
import org.slf4j.Logger
import org.slf4j.MarkerFactory

public fun Marker.toSlf4j(): org.slf4j.Marker = MarkerFactory.getMarker(this.getName())
public fun Marker.toSlf4j(): org.slf4j.Marker =
when (this) {
is Slf4jMarker -> marker
else -> MarkerFactory.getMarker(getName())
}

public fun org.slf4j.Marker.toKotlinLogging(): Marker = Slf4jMarker(this)

public fun Level.toSlf4j(): org.slf4j.event.Level =
when (this) {
Expand Down
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
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)
}
}
Loading