diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fa0dd7e7c..f20426040e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). * An enumeration class having a primary constructor and in which the list of enum entries is followed by a semicolon then do not remove the semicolon in case it is followed by code element `no-semi` ([#1733](https://github.com/pinterest/ktlint/issues/1733)) * Disable the `standard:filename` rule whenever Ktlint CLI is run with option `--stdin` ([#1742](https://github.com/pinterest/ktlint/issues/1742)) +* Fix initialization of the logger when `--log-level` is specified. Throw exception when an invalid value is passed. ([#1749](https://github.com/pinterest/ktlint/issues/1749)) ### Changed diff --git a/ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt b/ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt index 809b1297c0..05e7a3d5a9 100644 --- a/ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt +++ b/ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt @@ -21,6 +21,10 @@ public fun main(args: Array) { .setUsageHelpAutoWidth(true) val parseResult = commandLine.parseArgs(*args) + // The logger needs to be configured for the ktlintCommand and all subcommands. The logger can however not be configured before the + // commandline has been parsed as otherwise the loglevel conversion is not yet executed. + ktlintCommand.configureLogger() + commandLine.printCommandLineHelpOrVersionUsage() if (parseResult.hasSubcommand()) { diff --git a/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/KtlintCommandLine.kt b/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/KtlintCommandLine.kt index f9846c2b5b..00539d0aa0 100644 --- a/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/KtlintCommandLine.kt +++ b/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/KtlintCommandLine.kt @@ -272,23 +272,17 @@ internal class KtlintCommandLine { .map { ruleId -> createRuleExecutionEditorConfigProperty(ruleId) to RuleExecution.disabled } .toTypedArray() - init { + fun run() { if (debugOld != null || trace != null || verbose != null) { if (minLogLevel == Level.OFF) { minLogLevel = Level.ERROR } - configureLogger().error { + logger.error { "Options '--debug', '--trace', '--verbose' and '-v' are deprecated and replaced with option '--log-level=' or '-l='." } exitKtLintProcess(1) } - // Ensure that logger is initialized even when the run method is not executed because a subcommand like (--help) - // is executed so that method exitKtLintProcess only prints a log line when the appropriate loglevel is set. - logger = configureLogger() - } - - fun run() { assertStdinAndPatternsFromStdinOptionsMutuallyExclusive() val stdinPatterns: Set = readPatternsFromStdin() @@ -368,13 +362,14 @@ internal class KtlintCommandLine { jarFile.toURI().toURL() } - private fun configureLogger() = - KotlinLogging + internal fun configureLogger() { + logger = KotlinLogging .logger {} .setDefaultLoggerModifier { logger -> (logger.underlyingLogger as Logger).level = minLogLevel } .initKtLintKLogger() + } private fun assertStdinAndPatternsFromStdinOptionsMutuallyExclusive() { if (stdin && stdinDelimiter != null) { @@ -721,7 +716,7 @@ private class LogLevelConverter : CommandLine.ITypeConverter { "WARN" -> Level.WARN "ERROR" -> Level.ERROR "NONE" -> Level.OFF - else -> Level.INFO + else -> throw IllegalStateException("Invalid log level '$value'") } }