Skip to content

Commit 5b822c2

Browse files
Copilotoshai
andauthored
Fix native build error: update GraalVM substitution to target KotlinLoggingConfiguration
Agent-Logs-Url: https://github.com/oshai/kotlin-logging/sessions/fb12ce1d-eadb-48ae-8d8f-8cf8962c1db4 Co-authored-by: oshai <2683969+oshai@users.noreply.github.com>
1 parent e93a9e7 commit 5b822c2

2 files changed

Lines changed: 47 additions & 64 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package io.github.oshai.kotlinlogging.internal
2+
3+
import com.oracle.svm.core.annotate.Substitute
4+
import com.oracle.svm.core.annotate.TargetClass
5+
import com.oracle.svm.core.annotate.TargetElement
6+
import io.github.oshai.kotlinlogging.KLoggerFactory
7+
import io.github.oshai.kotlinlogging.jul.internal.JulLoggerFactory
8+
import io.github.oshai.kotlinlogging.slf4j.internal.Slf4jLoggerFactory
9+
import java.util.function.BooleanSupplier
10+
11+
/**
12+
* This class is a substitution class as described in
13+
* [https://build-native-java-apps.cc/developer-guide/substitution/]. It fixes an issue where the
14+
* native build (GraalVM native-image) fails if Logback is not on the classpath. See
15+
* [https://github.com/oshai/kotlin-logging/issues/465]. The weird-looking class name is according
16+
* to convention.
17+
*/
18+
@Suppress("unused")
19+
internal class Target_io_github_oshai_kotlinlogging_KotlinLoggingConfiguration {
20+
@TargetClass(
21+
className = "io.github.oshai.kotlinlogging.KotlinLoggingConfiguration",
22+
onlyWith = [LogbackNotOnClasspath::class],
23+
)
24+
companion object {
25+
@Substitute
26+
@TargetElement(name = "detectLogger")
27+
fun detectLogger(): KLoggerFactory {
28+
if (System.getProperty("kotlin-logging-to-jul") != null) {
29+
return JulLoggerFactory
30+
}
31+
// Intentionally leave out the logback branch as logback is not on the classpath.
32+
// default to SLF4J
33+
return Slf4jLoggerFactory
34+
}
35+
}
36+
}
37+
38+
internal class LogbackNotOnClasspath : BooleanSupplier {
39+
override fun getAsBoolean(): Boolean {
40+
try {
41+
Class.forName("ch.qos.logback.classic.LoggerContext")
42+
return false
43+
} catch (_: ClassNotFoundException) {
44+
return true
45+
}
46+
}
47+
}

src/jvmMain/kotlin/io/github/oshai/kotlinlogging/internal/Target_io_github_oshai_kotlinlogging_internal_KLoggerFactory.kt

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)