Skip to content

Commit 83fb270

Browse files
Allow to set a default logger modifier (#1427)
1 parent bfec80e commit 83fb270

2 files changed

Lines changed: 29 additions & 14 deletions

File tree

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,26 @@ 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-
* Initializes the logger. Optionally the logger can be modified using the [loggerModifier].
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.
18+
*/
19+
public lateinit var defaultLoggerModifier: (KLogger) -> Unit
20+
21+
/**
22+
* Initializes the logger with the [defaultLoggerModifier] when set.
23+
*/
24+
public fun KLogger.initKtLintKLogger(): KLogger {
25+
return if (::defaultLoggerModifier.isInitialized) {
26+
apply { defaultLoggerModifier(this) }
27+
} else {
28+
this
29+
}
30+
}
31+
32+
/**
33+
* Initializes the logger with the [loggerModifier].
1634
*/
1735
public fun KLogger.initKtLintKLogger(
18-
loggerModifier: (KLogger) -> Unit = { _ -> }
36+
loggerModifier: (KLogger) -> Unit
1937
): KLogger = apply { loggerModifier(this) }

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ 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
1617
import com.pinterest.ktlint.core.initKtLintKLogger
1718
import com.pinterest.ktlint.core.internal.containsLintError
1819
import com.pinterest.ktlint.core.internal.loadBaseline
@@ -254,7 +255,14 @@ class KtlintCommandLine {
254255
if (verbose) {
255256
debug = true
256257
}
257-
logger = configureLogger()
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()
258266

259267
failOnOldRulesetProviderUsage()
260268

@@ -294,17 +302,6 @@ class KtlintCommandLine {
294302
}
295303
}
296304

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

0 commit comments

Comments
 (0)