Skip to content

Commit 3e6096d

Browse files
authored
Fix #598: Allow external configuration of logStartupMessage via Properties and Env Vars (#602)
* Implement easily configurable logStartupMessage (#598) Addresses #598 by adding support for configuring KotlinLoggingConfiguration.logStartupMessage externally via System Properties and Environment Variables across all supported Kotlin Multiplatform platforms. Currently, disabling the startup message programmatically can be awkward or impossible if top-level loggers initialize KotlinLogging during class loading before main() or test setups have a chance to run. Providing external configuration flags completely bypasses this initialization order constraint. To maintain backward compatibility and avoid confusing users who rely on startup diagnostics, the default behavior remains true (opt-out). Cross-Platform Implementation Details The default value for logStartupMessage is now dynamically resolved during initialization based on the platform: JVM & Android: Checks the JVM System Property kotlin-logging.logStartupMessage first, then falls back to the Environment Variable KOTLIN_LOGGING_STARTUP_MESSAGE. Native & Darwin: Uses POSIX getenv to check the KOTLIN_LOGGING_STARTUP_MESSAGE environment variable. JS & Wasm JS: Safely evaluates the Browser environment (window.KOTLIN_LOGGING_STARTUP_MESSAGE) or Node.js environment (process.env.KOTLIN_LOGGING_STARTUP_MESSAGE). Returns false if either resolves to "false" or boolean false.
1 parent accd3ca commit 3e6096d

6 files changed

Lines changed: 80 additions & 6 deletions

File tree

src/androidMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ public actual object KotlinLoggingConfiguration {
3636

3737
@Volatile public actual var loggerFactory: KLoggerFactory = detectLogger()
3838

39-
public actual var logStartupMessage: Boolean = true
39+
public actual var logStartupMessage: Boolean =
40+
System.getProperty("kotlin-logging.logStartupMessage")?.toBoolean()
41+
?: System.getenv("KOTLIN_LOGGING_STARTUP_MESSAGE")?.toBoolean()
42+
?: true
4043

4144
private fun detectLogger(): KLoggerFactory {
4245
if (System.getProperty("kotlin-logging-to-android-native") != null) {

src/darwinMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
package io.github.oshai.kotlinlogging
22

33
import kotlin.concurrent.AtomicReference
4+
import kotlinx.cinterop.ExperimentalForeignApi
5+
import kotlinx.cinterop.toKString
6+
import platform.posix.getenv
7+
8+
@OptIn(ExperimentalForeignApi::class)
9+
private fun getEnv(name: String): String? {
10+
return getenv(name)?.toKString()
11+
}
412

513
public actual object KotlinLoggingConfiguration {
614
// Existing Darwin-specific properties
715
public var subsystem: AtomicReference<String?> = AtomicReference(null)
816
public var category: AtomicReference<String?> = AtomicReference(null)
917

10-
private val _logStartupMessage = AtomicReference(true)
18+
private val _logStartupMessage =
19+
AtomicReference(getEnv("KOTLIN_LOGGING_STARTUP_MESSAGE")?.toBoolean() ?: true)
1120
public actual var logStartupMessage: Boolean
1221
get() = _logStartupMessage.value
1322
set(value) {

src/jsMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
11
package io.github.oshai.kotlinlogging
22

3+
private fun resolveStartupMessageDefault(): Boolean {
4+
try {
5+
val disabledInWindow =
6+
js(
7+
"typeof window !== 'undefined' && (window.KOTLIN_LOGGING_STARTUP_MESSAGE === 'false' || window.KOTLIN_LOGGING_STARTUP_MESSAGE === false)"
8+
)
9+
as Boolean
10+
if (disabledInWindow) {
11+
return false
12+
}
13+
14+
val disabledInEnv =
15+
js(
16+
"typeof process !== 'undefined' && process.env && (process.env.KOTLIN_LOGGING_STARTUP_MESSAGE === 'false' || process.env.KOTLIN_LOGGING_STARTUP_MESSAGE === false)"
17+
)
18+
as Boolean
19+
if (disabledInEnv) {
20+
return false
21+
}
22+
23+
return true
24+
} catch (e: Throwable) {
25+
return true
26+
}
27+
}
28+
329
public actual object KotlinLoggingConfiguration {
4-
public actual var logStartupMessage: Boolean = true
30+
public actual var logStartupMessage: Boolean = resolveStartupMessageDefault()
531

632
public actual val direct: DirectLoggingConfiguration =
733
object : DirectLoggingConfiguration {

src/jvmMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ public actual object KotlinLoggingConfiguration {
1111
*/
1212
@Volatile public actual var loggerFactory: KLoggerFactory = detectLogger()
1313

14-
public actual var logStartupMessage: Boolean = true
14+
public actual var logStartupMessage: Boolean =
15+
System.getProperty("kotlin-logging.logStartupMessage")?.toBoolean()
16+
?: System.getenv("KOTLIN_LOGGING_STARTUP_MESSAGE")?.toBoolean()
17+
?: true
1518

1619
public actual val direct: DirectLoggingConfiguration =
1720
object : DirectLoggingConfiguration {

src/nativeMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
package io.github.oshai.kotlinlogging
22

33
import kotlin.concurrent.AtomicReference
4+
import kotlinx.cinterop.ExperimentalForeignApi
5+
import kotlinx.cinterop.toKString
6+
import platform.posix.getenv
7+
8+
@OptIn(ExperimentalForeignApi::class)
9+
private fun getEnv(name: String): String? {
10+
return getenv(name)?.toKString()
11+
}
412

513
public actual object KotlinLoggingConfiguration {
614
public actual val direct: DirectLoggingConfiguration =
@@ -31,7 +39,8 @@ public actual object KotlinLoggingConfiguration {
3139
}
3240
}
3341

34-
private val _logStartupMessage = AtomicReference(true)
42+
private val _logStartupMessage =
43+
AtomicReference(getEnv("KOTLIN_LOGGING_STARTUP_MESSAGE")?.toBoolean() ?: true)
3544
public actual var logStartupMessage: Boolean
3645
get() = _logStartupMessage.value
3746
set(value) {

src/wasmJsMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
package io.github.oshai.kotlinlogging
22

3+
private fun startupMessageDisabledInWindow(): Boolean =
4+
js(
5+
"typeof window !== 'undefined' && (window.KOTLIN_LOGGING_STARTUP_MESSAGE === 'false' || window.KOTLIN_LOGGING_STARTUP_MESSAGE === false)"
6+
)
7+
8+
private fun startupMessageDisabledInEnv(): Boolean =
9+
js(
10+
"typeof process !== 'undefined' && process.env && (process.env.KOTLIN_LOGGING_STARTUP_MESSAGE === 'false' || process.env.KOTLIN_LOGGING_STARTUP_MESSAGE === false)"
11+
)
12+
13+
private fun resolveStartupMessageDefault(): Boolean {
14+
try {
15+
if (startupMessageDisabledInWindow()) {
16+
return false
17+
}
18+
if (startupMessageDisabledInEnv()) {
19+
return false
20+
}
21+
return true
22+
} catch (e: Throwable) {
23+
return true
24+
}
25+
}
26+
327
public actual object KotlinLoggingConfiguration {
428
public actual val direct: DirectLoggingConfiguration =
529
object : DirectLoggingConfiguration {
@@ -36,5 +60,5 @@ public actual object KotlinLoggingConfiguration {
3660

3761
public actual var loggerFactory: KLoggerFactory = DirectLoggerFactory
3862

39-
public actual var logStartupMessage: Boolean = true
63+
public actual var logStartupMessage: Boolean = resolveStartupMessageDefault()
4064
}

0 commit comments

Comments
 (0)