Skip to content

Commit

Permalink
Fix initialization of the logger (#1750)
Browse files Browse the repository at this point in the history
* Fix initialization of the logger when `--log-level` is specified. Throw exception when an invalid value is passed.

Closes #1749
  • Loading branch information
paul-dingemans committed Dec 27, 2022
1 parent 0ce631d commit caa594a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,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))
* Add API so that KtLint API consumer is able to process a Kotlin script snippet without having to specify a file path ([#1738](https://github.com/pinterest/ktlint/issues/1738))
* 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

Expand Down
4 changes: 4 additions & 0 deletions ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt
Expand Up @@ -21,6 +21,10 @@ public fun main(args: Array<String>) {
.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()) {
Expand Down
Expand Up @@ -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=<level>' or '-l=<level>'."
}
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<String> = readPatternsFromStdin()
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -721,7 +716,7 @@ private class LogLevelConverter : CommandLine.ITypeConverter<Level> {
"WARN" -> Level.WARN
"ERROR" -> Level.ERROR
"NONE" -> Level.OFF
else -> Level.INFO
else -> throw IllegalStateException("Invalid log level '$value'")
}
}

Expand Down

0 comments on commit caa594a

Please sign in to comment.