Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix initialization of the logger #1750

Merged
merged 2 commits into from Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

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