From d57c87173b40efb32c0eb38fa093b785e16462a7 Mon Sep 17 00:00:00 2001 From: Sigmund Marius Nilssen Date: Thu, 28 Nov 2024 16:18:56 +0100 Subject: [PATCH] Add test of json output and arguments for Logback wrapper --- build.gradle.kts | 2 + .../oshai/kotlinlogging/AppenderWithWriter.kt | 2 + .../internal/LogbackLoggerWrapperTest.kt | 74 ++++++++++++++++++- 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 8a23476..e49fb73 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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"]}") diff --git a/src/jvmTest/kotlin/io/github/oshai/kotlinlogging/AppenderWithWriter.kt b/src/jvmTest/kotlin/io/github/oshai/kotlinlogging/AppenderWithWriter.kt index 6e5c80f..8117436 100644 --- a/src/jvmTest/kotlin/io/github/oshai/kotlinlogging/AppenderWithWriter.kt +++ b/src/jvmTest/kotlin/io/github/oshai/kotlinlogging/AppenderWithWriter.kt @@ -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 = diff --git a/src/jvmTest/kotlin/io/github/oshai/kotlinlogging/logback/internal/LogbackLoggerWrapperTest.kt b/src/jvmTest/kotlin/io/github/oshai/kotlinlogging/logback/internal/LogbackLoggerWrapperTest.kt index d1553e1..6643ca1 100644 --- a/src/jvmTest/kotlin/io/github/oshai/kotlinlogging/logback/internal/LogbackLoggerWrapperTest.kt +++ b/src/jvmTest/kotlin/io/github/oshai/kotlinlogging/logback/internal/LogbackLoggerWrapperTest.kt @@ -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 { @@ -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 + private lateinit var jsonAppender: OutputStreamAppender private lateinit var rootLogger: Logger @BeforeAll @@ -36,6 +44,18 @@ 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() appender.context = loggerContext @@ -43,8 +63,16 @@ class LogbackLoggerWrapperTest { appender.outputStream = logOutputStream appender.start() + jsonLogOutputStream = ByteArrayOutputStream() + jsonAppender = OutputStreamAppender() + 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 {} @@ -63,6 +91,12 @@ class LogbackLoggerWrapperTest { } } + @BeforeEach + fun resetTest() { + logOutputStream.reset() + jsonLogOutputStream.reset() + } + @Test fun testLogbackLogger() { assertTrue(logger is LogbackLoggerWrapper) @@ -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]) + } + }