Skip to content

Commit

Permalink
Add test of json output and arguments for Logback wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmanil committed Nov 28, 2024
1 parent 6760811 commit d57c871
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ kotlin {
implementation("org.apache.logging.log4j:log4j-slf4j2-impl:${extra["log4j_version"]}")
implementation("org.slf4j:slf4j-api:${extra["slf4j_version"]}")
implementation("ch.qos.logback:logback-classic:${extra["logback_version"]}")
implementation("net.logstash.logback:logstash-logback-encoder:8.0")

// our jul test just forward the logs jul -> slf4j -> log4j
implementation("org.slf4j:jul-to-slf4j:${extra["slf4j_version"]}")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-slf4j:${extra["coroutines_version"]}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import java.io.StringWriter
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.core.Appender
import org.apache.logging.log4j.core.appender.WriterAppender
import org.apache.logging.log4j.core.config.json.JsonConfiguration
import org.apache.logging.log4j.core.layout.JsonLayout
import org.apache.logging.log4j.core.layout.PatternLayout

fun newPatternLayout(pattern: String): PatternLayout =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.OutputStreamAppender
import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KotlinLogging
import net.logstash.logback.argument.StructuredArguments
import net.logstash.logback.composite.JsonProviders
import net.logstash.logback.composite.loggingevent.ArgumentsJsonProvider
import net.logstash.logback.composite.loggingevent.LoggingEventPatternJsonProvider
import net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder
import java.io.ByteArrayOutputStream
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

class LogbackLoggerWrapperTest {
Expand All @@ -20,7 +26,9 @@ class LogbackLoggerWrapperTest {
private lateinit var warnLogger: KLogger
private lateinit var errorLogger: KLogger
private lateinit var logOutputStream: ByteArrayOutputStream
private lateinit var jsonLogOutputStream: ByteArrayOutputStream
private lateinit var appender: OutputStreamAppender<ILoggingEvent>
private lateinit var jsonAppender: OutputStreamAppender<ILoggingEvent>
private lateinit var rootLogger: Logger

@BeforeAll
Expand All @@ -36,15 +44,35 @@ class LogbackLoggerWrapperTest {
encoder.charset = Charsets.UTF_8
encoder.start()

val jsonEncoder = LoggingEventCompositeJsonEncoder()
jsonEncoder.context = loggerContext
val patternProvider = LoggingEventPatternJsonProvider()
patternProvider.context = loggerContext
patternProvider.pattern = """{"message": "%message"}"""
jsonEncoder.providers.addProvider(patternProvider)
val argumentsJsonProvider = ArgumentsJsonProvider()
argumentsJsonProvider.isIncludeStructuredArguments = true
argumentsJsonProvider.nonStructuredArgumentsFieldPrefix = ""
jsonEncoder.providers.addProvider(argumentsJsonProvider)
jsonEncoder.start()

logOutputStream = ByteArrayOutputStream()
appender = OutputStreamAppender<ILoggingEvent>()
appender.context = loggerContext
appender.encoder = encoder
appender.outputStream = logOutputStream
appender.start()

jsonLogOutputStream = ByteArrayOutputStream()
jsonAppender = OutputStreamAppender<ILoggingEvent>()
jsonAppender.context = loggerContext
jsonAppender.encoder = jsonEncoder
jsonAppender.outputStream = jsonLogOutputStream
jsonAppender.start()

rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME)
rootLogger.addAppender(appender)
rootLogger.addAppender(jsonAppender)
rootLogger.level = Level.TRACE

logger = KotlinLogging.logger {}
Expand All @@ -63,6 +91,12 @@ class LogbackLoggerWrapperTest {
}
}

@BeforeEach
fun resetTest() {
logOutputStream.reset()
jsonLogOutputStream.reset()
}

@Test
fun testLogbackLogger() {
assertTrue(logger is LogbackLoggerWrapper)
Expand All @@ -76,14 +110,48 @@ class LogbackLoggerWrapperTest {
.toByteArray()
.toString(Charsets.UTF_8)
.trim()
.replace("\r", "\n")
.replace("\n\n", "\n")
.split("\n")
.lines()
val jsonLines = jsonLogOutputStream
.toByteArray()
.toString(Charsets.UTF_8)
.trim()
.lines()
assertEquals(
"INFO io.github.oshai.kotlinlogging.logback.internal.LogbackLoggerWrapperTest - simple logback info message",
lines[0],
)
assertEquals(
"""{"message":"simple logback info message"}""",
jsonLines[0]
)
assertEquals("WARN warnLogger - simple logback warn message", lines[1])
assertEquals("""{"message":"simple logback warn message"}""", jsonLines[1])
assertEquals("ERROR errorLogger - simple logback error message", lines[2])
assertEquals("""{"message":"simple logback error message"}""",jsonLines[2])
}

@Test
fun testLogbackLoggerWithArguments() {
logger.atInfo {
message = "msg"
arguments = arrayOf(StructuredArguments.keyValue("arg1", "val1"), StructuredArguments.keyValue("arg2", "val2"))
}
val lines =
logOutputStream
.toByteArray()
.toString(Charsets.UTF_8)
.trim()
.lines()
val jsonLines = jsonLogOutputStream
.toByteArray()
.toString(Charsets.UTF_8)
.trim()
.lines()

assertEquals(1, lines.size)
assertEquals(1, jsonLines.size)
assertEquals("INFO io.github.oshai.kotlinlogging.logback.internal.LogbackLoggerWrapperTest - msg", lines[0])
assertEquals("""{"message":"msg","arg1":"val1","arg2":"val2"}""",jsonLines[0])
}

}

0 comments on commit d57c871

Please sign in to comment.