Skip to content

Commit 06f6e71

Browse files
lbilgerLars Bilger
and
Lars Bilger
authored
Fix native image compilation when Logback is not on the classpath (#491)
* Fix native image compilation when Logback is not on the classpath Fixes #465 --------- Co-authored-by: Lars Bilger <[email protected]>
1 parent 730e279 commit 06f6e71

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ kotlin {
144144
compileOnly("org.slf4j:slf4j-api:${extra["slf4j_version"]}")
145145
compileOnly("ch.qos.logback:logback-classic:${extra["logback_version"]}")
146146
compileOnly("org.jetbrains.kotlinx:kotlinx-coroutines-slf4j:${extra["coroutines_version"]}")
147+
compileOnly("org.graalvm.sdk:nativeimage:${extra["nativeimage_version"]}")
147148
}
148149
}
149150
val jvmLogbackTest by getting {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ internal actual object KLoggerFactory {
1010

1111
/** get logger by explicit name */
1212
internal actual fun logger(name: String): KLogger {
13+
// Note: any changes here might have to be also applied to
14+
// [Target_io_github_oshai_kotlinlogging_internal_KLoggerFactory].
1315
if (System.getProperty("kotlin-logging-to-jul") != null) {
1416
return JulLoggerFactory.wrapJLogger(JulLoggerFactory.jLogger(name))
1517
} else if (System.getProperty("kotlin-logging-to-logback") == "true") {
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.KLogger
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_internal_KLoggerFactory {
20+
@TargetClass(
21+
className = "io.github.oshai.kotlinlogging.internal.KLoggerFactory",
22+
onlyWith = [LogbackNotOnClasspath::class],
23+
)
24+
companion object {
25+
@Substitute
26+
@TargetElement(name = "logger\$kotlin_logging")
27+
fun logger(name: String): KLogger {
28+
if (System.getProperty("kotlin-logging-to-jul") != null) {
29+
return JulLoggerFactory.wrapJLogger(JulLoggerFactory.jLogger(name))
30+
}
31+
// Intentionally leave out the logback branch as logback is not on the classpath.
32+
// default to SLF4J
33+
return Slf4jLoggerFactory.wrapJLogger(Slf4jLoggerFactory.jLogger(name))
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+
}

versions.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ extra["junit_version"] = "5.9.2"
66
extra["logback_version"] = "1.5.11"
77
extra["logstash_logback_encoder_version"] = "8.0"
88
extra["jackson_version"] = "2.18.3"
9+
extra["nativeimage_version"] = "24.2.0"

0 commit comments

Comments
 (0)