Skip to content

Commit

Permalink
Support arguments where possible for Logback and Slf4j
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmanil committed Nov 27, 2024
1 parent 1f9ecdd commit 6760811
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class KLoggingEventBuilder {
public var message: String? = null
public var cause: Throwable? = null
public var payload: Map<String, Any?>? = null
public var arguments: Array<Any>? = null

/**
* Internal data that is used by compiler plugin to provide additional information about the log
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ internal class LocationAwareKLogger(override val underlyingLogger: LocationAware
val builder = underlyingLogger.atLevel(level.toSlf4j())
marker?.toSlf4j()?.let { builder.addMarker(it) }
kLoggingEventBuilder.payload?.forEach { (key, value) -> builder.addKeyValue(key, value) }
kLoggingEventBuilder.arguments?.forEach { arg -> builder.addArgument(arg) }
builder.setCause(kLoggingEventBuilder.cause)
if (builder is CallerBoundaryAware) {
builder.setCallerBoundary(fqcn)
Expand All @@ -75,7 +76,7 @@ internal class LocationAwareKLogger(override val underlyingLogger: LocationAware
fqcn,
level.toSlf4j().toInt(),
kLoggingEventBuilder.message,
null,
kLoggingEventBuilder.arguments,
kLoggingEventBuilder.cause,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ internal class LocationIgnorantKLogger(override val underlyingLogger: Logger) :
val builder = underlyingLogger.atLevel(level.toSlf4j())
marker?.toSlf4j()?.let { builder.addMarker(it) }
kLoggingEventBuilder.payload?.forEach { (key, value) -> builder.addKeyValue(key, value) }
kLoggingEventBuilder.arguments?.forEach { arg -> builder.addArgument(arg) }
builder.setCause(kLoggingEventBuilder.cause)
builder.log(kLoggingEventBuilder.message)
}
Expand All @@ -55,6 +56,7 @@ internal class LocationIgnorantKLogger(override val underlyingLogger: Logger) :
val slf4jMarker = marker?.toSlf4j()
val message = kLoggingEventBuilder.message
val cause = kLoggingEventBuilder.cause

when (level) {
Level.TRACE -> underlyingLogger.trace(slf4jMarker, message, cause)
Level.DEBUG -> underlyingLogger.debug(slf4jMarker, message, cause)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class LogbackLogEvent(
level.toLogbackLevel(),
kLoggingEvent.internalCompilerData?.messageTemplate ?: kLoggingEvent.message,
kLoggingEvent.cause,
emptyArray(),
kLoggingEvent.arguments ?: emptyArray(),
) {

override fun getFormattedMessage(): String? {
Expand Down

0 comments on commit 6760811

Please sign in to comment.