Skip to content

Commit 5fd23c0

Browse files
logger initialization improvement (#1430)
Hide variable defaultLoggerModifier and enforce that it can be set via function setDefaultLoggerModifier only. In this way, a warning can be printed if it is set more than once which could lead to unpredictable behavior.
1 parent 83fb270 commit 5fd23c0

3 files changed

Lines changed: 46 additions & 25 deletions

File tree

ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/KtLintKLoggerInitializer.kt

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,41 @@ public const val KTLINT_UNIT_TEST_DUMP_AST = "KTLINT_UNIT_TEST_DUMP_AST"
1212
public const val KTLINT_UNIT_TEST_ON_PROPERTY = "ON"
1313

1414
/**
15-
* Default modifier for the KLogger of new instances of classes calling [initKtLintKLogger]. Classes for which
16-
* [initKtLintKLogger] has been called before setting this variable will not be changed. Also note, that this modifier
17-
* can only be set once.
15+
* Default modifier for the KLogger. It can be set only once via [setDefaultLoggerModifier] but it should be set before
16+
* the first invocation of [initKtLintKLogger].
1817
*/
19-
public lateinit var defaultLoggerModifier: (KLogger) -> Unit
18+
private var defaultLoggerModifier: ((KLogger) -> Unit)? = null
2019

2120
/**
22-
* Initializes the logger with the [defaultLoggerModifier] when set.
21+
* Set the [defaultLoggerModifier]. Note that it can only be set once. It should be set before the first invocation to
22+
* [initKtLintKLogger].
2323
*/
24-
public fun KLogger.initKtLintKLogger(): KLogger {
25-
return if (::defaultLoggerModifier.isInitialized) {
26-
apply { defaultLoggerModifier(this) }
27-
} else {
28-
this
24+
public fun KLogger.setDefaultLoggerModifier(
25+
loggerModifier: (KLogger) -> Unit
26+
): KLogger {
27+
if (defaultLoggerModifier != null) {
28+
warn {
29+
"""
30+
The defaultLoggerModifier has been set before and might already have been applied when initializing
31+
Loggers. Loggers which will be initialized after resetting the defaultLoggerModifier will be initialized
32+
with the new value. This might result in unpredictable behavior. Except for in unit tests, it is
33+
recommended to ensure to call this function only once.
34+
""".trimIndent()
35+
}
2936
}
37+
defaultLoggerModifier = loggerModifier
38+
return this
3039
}
3140

3241
/**
33-
* Initializes the logger with the [loggerModifier].
42+
* Initializes the logger with the [defaultLoggerModifier].
3443
*/
35-
public fun KLogger.initKtLintKLogger(
36-
loggerModifier: (KLogger) -> Unit
37-
): KLogger = apply { loggerModifier(this) }
44+
public fun KLogger.initKtLintKLogger(): KLogger {
45+
if (defaultLoggerModifier == null) {
46+
// Initialize the defaultLoggerModifier on the first invocation of initKtlintLogger when it is not yet set.
47+
// In this way it can be ensured that all loggers are initialized with the exact same logger modifier.
48+
defaultLoggerModifier = { _ -> }
49+
}
50+
51+
return apply { defaultLoggerModifier?.invoke(this) }
52+
}

ktlint-test/src/main/kotlin/com/pinterest/ktlint/test/RuleExtension.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.pinterest.ktlint.core.Rule
66
import com.pinterest.ktlint.core.RuleSet
77
import com.pinterest.ktlint.core.api.FeatureInAlphaState
88
import com.pinterest.ktlint.core.initKtLintKLogger
9+
import com.pinterest.ktlint.core.setDefaultLoggerModifier
910
import com.pinterest.ruleset.test.DumpASTRule
1011
import mu.KotlinLogging
1112
import org.assertj.core.api.Assertions.assertThat
@@ -16,7 +17,7 @@ import org.jetbrains.kotlin.utils.addToStdlib.ifTrue
1617
private val logger =
1718
KotlinLogging
1819
.logger {}
19-
.initKtLintKLogger { logger ->
20+
.setDefaultLoggerModifier { logger ->
2021
if (!logger.isTraceEnabled || !logger.isDebugEnabled) {
2122
logger.info {
2223
"""
@@ -26,7 +27,7 @@ private val logger =
2627
""".trimIndent()
2728
}
2829
}
29-
}
30+
}.initKtLintKLogger()
3031

3132
// Via command line parameter "--trace" the end user of ktlint can change the logging behavior. As unit tests are not
3233
// invoked via the main ktlint runtime, this command line parameter can not be used to change the logging behavior while

ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import com.pinterest.ktlint.core.RuleExecutionException
1313
import com.pinterest.ktlint.core.RuleSet
1414
import com.pinterest.ktlint.core.RuleSetProvider
1515
import com.pinterest.ktlint.core.VisitorProvider
16-
import com.pinterest.ktlint.core.defaultLoggerModifier
1716
import com.pinterest.ktlint.core.initKtLintKLogger
1817
import com.pinterest.ktlint.core.internal.containsLintError
1918
import com.pinterest.ktlint.core.internal.loadBaseline
2019
import com.pinterest.ktlint.core.internal.relativeRoute
20+
import com.pinterest.ktlint.core.setDefaultLoggerModifier
2121
import com.pinterest.ktlint.internal.ApplyToIDEAGloballySubCommand
2222
import com.pinterest.ktlint.internal.ApplyToIDEAProjectSubCommand
2323
import com.pinterest.ktlint.internal.GenerateEditorConfigSubCommand
@@ -255,14 +255,7 @@ class KtlintCommandLine {
255255
if (verbose) {
256256
debug = true
257257
}
258-
defaultLoggerModifier = { logger ->
259-
(logger.underlyingLogger as Logger).level = when {
260-
trace -> Level.TRACE
261-
debug -> Level.DEBUG
262-
else -> Level.INFO
263-
}
264-
}
265-
logger = KotlinLogging.logger {}.initKtLintKLogger()
258+
logger = configureLogger()
266259

267260
failOnOldRulesetProviderUsage()
268261

@@ -302,6 +295,18 @@ class KtlintCommandLine {
302295
}
303296
}
304297

298+
private fun configureLogger() =
299+
KotlinLogging
300+
.logger {}
301+
.setDefaultLoggerModifier { logger ->
302+
(logger.underlyingLogger as Logger).level = when {
303+
trace -> Level.TRACE
304+
debug -> Level.DEBUG
305+
else -> Level.INFO
306+
}
307+
}
308+
.initKtLintKLogger()
309+
305310
private fun lintFiles(
306311
ruleSetProviders: Map<String, RuleSetProvider>,
307312
visitorProvider: VisitorProvider,

0 commit comments

Comments
 (0)