From 86456150045fa20c3d1da6c8f742a1f73cd3451a Mon Sep 17 00:00:00 2001 From: oshai Date: Wed, 12 Jul 2023 21:58:08 +0300 Subject: [PATCH 1/3] add failing test for https://github.com/apache/logging-log4j2/issues/1533 --- .../kotlinlogging/ClassWithLoggingForLocationTesting.kt | 7 +++++++ .../oshai/kotlinlogging/LoggingWithLocationTest.kt | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/src/jvmTest/kotlin/io/github/oshai/kotlinlogging/ClassWithLoggingForLocationTesting.kt b/src/jvmTest/kotlin/io/github/oshai/kotlinlogging/ClassWithLoggingForLocationTesting.kt index bf9440b2..f5dc1075 100644 --- a/src/jvmTest/kotlin/io/github/oshai/kotlinlogging/ClassWithLoggingForLocationTesting.kt +++ b/src/jvmTest/kotlin/io/github/oshai/kotlinlogging/ClassWithLoggingForLocationTesting.kt @@ -27,4 +27,11 @@ class ClassWithLoggingForLocationTesting { logger.info { "log entry body" } return logger.exit(null) } + + fun logFluentWithPayload() { + logger.atInfo { + message = "test" + payload = mapOf("key" to "value") + } + } } diff --git a/src/jvmTest/kotlin/io/github/oshai/kotlinlogging/LoggingWithLocationTest.kt b/src/jvmTest/kotlin/io/github/oshai/kotlinlogging/LoggingWithLocationTest.kt index 518cf9c8..726958b8 100644 --- a/src/jvmTest/kotlin/io/github/oshai/kotlinlogging/LoggingWithLocationTest.kt +++ b/src/jvmTest/kotlin/io/github/oshai/kotlinlogging/LoggingWithLocationTest.kt @@ -42,6 +42,15 @@ class LoggingWithLocationTest { ) } + @Test + fun testFluentLoggingWithLocation() { + ClassWithLoggingForLocationTesting().logFluentWithPayload() + assertEquals( + "INFO ClassWithLoggingForLocationTesting.logFluentWithPayload(32) - test", + appenderWithWriter.writer.toString().trim() + ) + } + @Test fun testNullLoggingWithLocation() { ClassWithLoggingForLocationTesting().logNull() From 709aea771d4f926375ca2fc90571cb418cd3a77c Mon Sep 17 00:00:00 2001 From: oshai Date: Mon, 27 Nov 2023 09:50:38 +0200 Subject: [PATCH 2/3] upgrade logj4 to 2.22.0 --- versions.gradle.kts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/versions.gradle.kts b/versions.gradle.kts index cef1d1a3..b7f0e927 100644 --- a/versions.gradle.kts +++ b/versions.gradle.kts @@ -1,5 +1,5 @@ extra["slf4j_version"] = "2.0.6" extra["coroutines_version"] = "1.6.4" -extra["log4j_version"] = "2.20.0" +extra["log4j_version"] = "2.22.0" extra["mockito_version"] = "4.11.0" -extra["junit_version"] = "5.9.2" \ No newline at end of file +extra["junit_version"] = "5.9.2" From 3d4d865d3ae1ef026c73b60811b63763c8baa4ec Mon Sep 17 00:00:00 2001 From: oshai Date: Mon, 27 Nov 2023 11:24:39 +0200 Subject: [PATCH 3/3] remove default params from overloads add override methods in location aware. --- .editorconfig | 1 - .../io/github/oshai/kotlinlogging/KLogger.kt | 25 +++++++++++++++---- .../slf4j/internal/LocationAwareKLogger.kt | 12 +++++++++ versions.gradle.kts | 2 +- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/.editorconfig b/.editorconfig index aab17e9e..392fe661 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,7 +8,6 @@ max_line_length = 150 ; Tab indentation (no size specified) indent_style = space charset = utf-8 -indent_size = 4 [*.yml] indent_size = 2 diff --git a/src/commonMain/kotlin/io/github/oshai/kotlinlogging/KLogger.kt b/src/commonMain/kotlin/io/github/oshai/kotlinlogging/KLogger.kt index 109fba4e..6a821e46 100644 --- a/src/commonMain/kotlin/io/github/oshai/kotlinlogging/KLogger.kt +++ b/src/commonMain/kotlin/io/github/oshai/kotlinlogging/KLogger.kt @@ -109,25 +109,40 @@ public interface KLogger { } /** Lazy add a log message with throwable payload if isTraceEnabled is true */ - public fun atTrace(marker: Marker? = null, block: KLoggingEventBuilder.() -> Unit): Unit = + public fun atTrace(marker: Marker?, block: KLoggingEventBuilder.() -> Unit): Unit = at(Level.TRACE, marker, block) + /** Lazy add a log message with throwable payload if isTraceEnabled is true */ + public fun atTrace(block: KLoggingEventBuilder.() -> Unit): Unit = at(Level.TRACE, null, block) + /** Lazy add a log message with throwable payload if isDebugEnabled is true */ - public fun atDebug(marker: Marker? = null, block: KLoggingEventBuilder.() -> Unit): Unit = + public fun atDebug(marker: Marker?, block: KLoggingEventBuilder.() -> Unit): Unit = at(Level.DEBUG, marker, block) + /** Lazy add a log message with throwable payload if isDebugEnabled is true */ + public fun atDebug(block: KLoggingEventBuilder.() -> Unit): Unit = at(Level.DEBUG, null, block) + /** Lazy add a log message with throwable payload if isInfoEnabled is true */ - public fun atInfo(marker: Marker? = null, block: KLoggingEventBuilder.() -> Unit): Unit = + public fun atInfo(marker: Marker?, block: KLoggingEventBuilder.() -> Unit): Unit = at(Level.INFO, marker, block) + /** Lazy add a log message with throwable payload if isInfoEnabled is true */ + public fun atInfo(block: KLoggingEventBuilder.() -> Unit): Unit = at(Level.INFO, null, block) + /** Lazy add a log message with throwable payload if isWarnEnabled is true */ - public fun atWarn(marker: Marker? = null, block: KLoggingEventBuilder.() -> Unit): Unit = + public fun atWarn(marker: Marker?, block: KLoggingEventBuilder.() -> Unit): Unit = at(Level.WARN, marker, block) + /** Lazy add a log message with throwable payload if isWarnEnabled is true */ + public fun atWarn(block: KLoggingEventBuilder.() -> Unit): Unit = at(Level.WARN, null, block) + /** Lazy add a log message with throwable payload if isErrorEnabled is true */ - public fun atError(marker: Marker? = null, block: KLoggingEventBuilder.() -> Unit): Unit = + public fun atError(marker: Marker?, block: KLoggingEventBuilder.() -> Unit): Unit = at(Level.ERROR, marker, block) + /** Lazy add a log message with throwable payload if isErrorEnabled is true */ + public fun atError(block: KLoggingEventBuilder.() -> Unit): Unit = at(Level.ERROR, null, block) + /** Lazy add a log message if level enabled */ public fun at(level: Level, marker: Marker? = null, block: KLoggingEventBuilder.() -> Unit) diff --git a/src/javaMain/kotlin/io/github/oshai/kotlinlogging/slf4j/internal/LocationAwareKLogger.kt b/src/javaMain/kotlin/io/github/oshai/kotlinlogging/slf4j/internal/LocationAwareKLogger.kt index 3dbeec27..b1b1e9b6 100644 --- a/src/javaMain/kotlin/io/github/oshai/kotlinlogging/slf4j/internal/LocationAwareKLogger.kt +++ b/src/javaMain/kotlin/io/github/oshai/kotlinlogging/slf4j/internal/LocationAwareKLogger.kt @@ -271,15 +271,27 @@ internal class LocationAwareKLogger(override val underlyingLogger: LocationAware override fun atDebug(marker: Marker?, block: KLoggingEventBuilder.() -> Unit): Unit = at(Level.DEBUG, marker, block) + /** Lazy add a log message with throwable payload if isDebugEnabled is true */ + override fun atDebug(block: KLoggingEventBuilder.() -> Unit): Unit = at(Level.DEBUG, null, block) + /** Lazy add a log message with throwable payload if isInfoEnabled is true */ override fun atInfo(marker: Marker?, block: KLoggingEventBuilder.() -> Unit): Unit = at(Level.INFO, marker, block) + /** Lazy add a log message with throwable payload if isInfoEnabled is true */ + override fun atInfo(block: KLoggingEventBuilder.() -> Unit): Unit = at(Level.INFO, null, block) + /** Lazy add a log message with throwable payload if isWarnEnabled is true */ override fun atWarn(marker: Marker?, block: KLoggingEventBuilder.() -> Unit): Unit = at(Level.WARN, marker, block) + /** Lazy add a log message with throwable payload if isWarnEnabled is true */ + override fun atWarn(block: KLoggingEventBuilder.() -> Unit): Unit = at(Level.WARN, null, block) + /** Lazy add a log message with throwable payload if isErrorEnabled is true */ override fun atError(marker: Marker?, block: KLoggingEventBuilder.() -> Unit): Unit = at(Level.ERROR, marker, block) + + /** Lazy add a log message with throwable payload if isErrorEnabled is true */ + override fun atError(block: KLoggingEventBuilder.() -> Unit): Unit = at(Level.ERROR, null, block) } diff --git a/versions.gradle.kts b/versions.gradle.kts index b7f0e927..2b1f4132 100644 --- a/versions.gradle.kts +++ b/versions.gradle.kts @@ -1,4 +1,4 @@ -extra["slf4j_version"] = "2.0.6" +extra["slf4j_version"] = "2.0.9" extra["coroutines_version"] = "1.6.4" extra["log4j_version"] = "2.22.0" extra["mockito_version"] = "4.11.0"