Skip to content

Commit

Permalink
Populate caller information from compiler data, if available
Browse files Browse the repository at this point in the history
  • Loading branch information
Neeme Praks committed Oct 28, 2024
1 parent 797b15d commit c7047dc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
package io.github.oshai.kotlinlogging.logback.internal

import ch.qos.logback.classic.Level
import ch.qos.logback.classic.Logger
import ch.qos.logback.classic.spi.LoggingEvent
import io.github.oshai.kotlinlogging.KLoggingEventBuilder
import io.github.oshai.kotlinlogging.Level
import io.github.oshai.kotlinlogging.logback.toLogbackLevel

public class LogbackLogEvent(
fqcn: String,
logger: Logger,
level: Level,
message: String?,
private val finalFormattedMessage: String?,
throwable: Throwable?,
argArray: Array<Any>,
) : LoggingEvent(fqcn, logger, level, message, throwable, argArray) {
private val kLoggingEvent: KLoggingEventBuilder,
) :
LoggingEvent(
fqcn,
logger,
level.toLogbackLevel(),
kLoggingEvent.internalCompilerData?.messageTemplate ?: kLoggingEvent.message,
kLoggingEvent.cause,
emptyArray(),
) {

override fun getFormattedMessage(): String? {
return finalFormattedMessage
return kLoggingEvent.message
}

override fun getCallerData(): Array<StackTraceElement> =
if (kLoggingEvent.internalCompilerData?.fileName != null) {
arrayOf(
StackTraceElement(
kLoggingEvent.internalCompilerData?.className,
kLoggingEvent.internalCompilerData?.methodName,
kLoggingEvent.internalCompilerData?.fileName,
kLoggingEvent.internalCompilerData?.lineNumber ?: 0,
)
)
} else {
super.getCallerData()
}

override fun hasCallerData(): Boolean =
if (kLoggingEvent.internalCompilerData?.fileName != null) {
true
} else {
super.hasCallerData()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ internal class LogbackLoggerWrapper(
LogbackLogEvent(
fqcn = fqcn,
logger = underlyingLogger,
level = level.toLogbackLevel(),
message = internalCompilerData?.messageTemplate ?: message,
finalFormattedMessage = message,
throwable = cause,
argArray = emptyArray(),
level = level,
kLoggingEvent = this,
)
marker?.toLogback(logbackServiceProvider)?.let { logbackEvent.addMarker(it) }
payload?.forEach { (key, value) -> logbackEvent.addKeyValuePair(KeyValuePair(key, value)) }
Expand Down

0 comments on commit c7047dc

Please sign in to comment.