Skip to content

Commit 5bc77a0

Browse files
committed
fix throwable code
1 parent e4d29a2 commit 5bc77a0

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/nativeMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerNameResolver.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@ internal actual object KLoggerNameResolver {
2323
// Fallback for top-level loggers: Parse the stack trace string array.
2424
val stackTrace: Array<String> = Throwable().getStackTrace()
2525

26-
// Find the call to this function as a reliable anchor.
27-
val resolverCallIndex =
28-
stackTrace.indexOfFirst {
29-
it.contains("io.github.oshai.kotlinlogging.internal.KLoggerNameResolver#name")
26+
// Find the first frame outside of the logging framework's internal machinery.
27+
val callerFrame =
28+
stackTrace.firstOrNull {
29+
!it.contains("kfun:kotlin.Throwable") &&
30+
!it.contains("io.github.oshai.kotlinlogging.internal") &&
31+
!it.contains("io.github.oshai.kotlinlogging.KotlinLogging")
3032
}
3133

32-
// The caller is two frames up from this function in the stack trace.
33-
val callerFrameIndex = resolverCallIndex + 2
34-
if (resolverCallIndex != -1 && callerFrameIndex < stackTrace.size) {
35-
val callerFrame = stackTrace[callerFrameIndex]
34+
if (callerFrame != null) {
3635
val regex = Regex("""kfun:([^#(<]+)""")
3736
val fqName = regex.find(callerFrame)?.groupValues?.get(1)?.trim()
3837
if (fqName != null) {

0 commit comments

Comments
 (0)