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

add payload to LocationAwareKLogger #332

Merged
merged 4 commits into from
Nov 27, 2023
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
1 change: 0 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 20 additions & 5 deletions src/commonMain/kotlin/io/github/oshai/kotlinlogging/KLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions versions.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extra["slf4j_version"] = "2.0.6"
extra["slf4j_version"] = "2.0.9"
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"
extra["junit_version"] = "5.9.2"
Loading