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

Use ktlint's mechanism to initialize loggers #1414

Merged
merged 10 commits into from
Jun 30, 2022
9 changes: 9 additions & 0 deletions diktat-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
</dependency>
<dependency>
<groupId>io.github.microutils</groupId>
<artifactId>kotlin-logging-jvm</artifactId>
</dependency>
<!-- ktlint-core is needed only for `initKtLintKLogger` method -->
<dependency>
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-core</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.cqfn.diktat.common.config.reader

import com.pinterest.ktlint.core.initKtLintKLogger
import mu.KotlinLogging
import org.slf4j.LoggerFactory

import java.io.IOException
Expand Down Expand Up @@ -35,7 +37,9 @@ open class ApplicationProperties(propertiesFileName: String) {
}

companion object {
private val log = LoggerFactory.getLogger(ApplicationProperties::class.java)
private val log = KotlinLogging.logger(
LoggerFactory.getLogger(ApplicationProperties::class.java)
).initKtLintKLogger()
private const val EXIT_STATUS_MISSING_PROPERTIES = 4
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.cqfn.diktat.common.config.reader

import com.pinterest.ktlint.core.initKtLintKLogger
import mu.KotlinLogging
import org.slf4j.Logger
import org.slf4j.LoggerFactory

Expand Down Expand Up @@ -60,6 +62,8 @@ abstract class JsonResourceConfigReader<T> {
/**
* A [Logger] that can be used
*/
val log: Logger = LoggerFactory.getLogger(JsonResourceConfigReader::class.java)
val log: Logger = KotlinLogging.logger(
LoggerFactory.getLogger(JsonResourceConfigReader::class.java)
).initKtLintKLogger()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import org.cqfn.diktat.common.config.rules.RulesConfigReader.Companion.log

import com.charleskorn.kaml.Yaml
import com.charleskorn.kaml.YamlConfiguration
import com.pinterest.ktlint.core.initKtLintKLogger
import mu.KotlinLogging
import org.slf4j.Logger
import org.slf4j.LoggerFactory

Expand Down Expand Up @@ -110,7 +112,9 @@ open class RulesConfigReader(override val classLoader: ClassLoader) : JsonResour
/**
* A [Logger] that can be used
*/
val log: Logger = LoggerFactory.getLogger(RulesConfigReader::class.java)
val log: Logger = KotlinLogging.logger(
LoggerFactory.getLogger(RulesConfigReader::class.java)
).initKtLintKLogger()
}
}

Expand Down
4 changes: 4 additions & 0 deletions diktat-rules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>io.github.microutils</groupId>
<artifactId>kotlin-logging-jvm</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import org.cqfn.diktat.ruleset.constants.EmitType
import org.cqfn.diktat.ruleset.utils.getFilePath

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.initKtLintKLogger
import mu.KotlinLogging
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.slf4j.LoggerFactory

Expand Down Expand Up @@ -75,6 +77,8 @@ abstract class DiktatRule(
abstract fun logic(node: ASTNode)

companion object {
private val log = LoggerFactory.getLogger(DiktatRule::class.java)
private val log = KotlinLogging.logger(
LoggerFactory.getLogger(DiktatRule::class.java)
).initKtLintKLogger()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,16 @@ import org.cqfn.diktat.ruleset.rules.chapter6.classes.StatelessClassesRule

import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.RuleSetProvider
import com.pinterest.ktlint.core.initKtLintKLogger
import mu.KotlinLogging
import org.jetbrains.kotlin.org.jline.utils.Levenshtein
import org.slf4j.LoggerFactory

import java.io.File

/**
* [RuleSetProvider] that provides diKTat ruleset.
* By default it is expected to have diktat-analysis.yml configuration in the root folder where 'ktlint' is run
* By default, it is expected to have diktat-analysis.yml configuration in the root folder where 'ktlint' is run
* otherwise it will use default configuration where some rules are disabled
*
* @param diktatConfigFile - configuration file where all configurations for inspections and rules are stored
Expand All @@ -107,7 +109,6 @@ class DiktatRuleSetProvider(private var diktatConfigFile: String = DIKTAT_ANALYS
@Suppress(
"LongMethod",
"TOO_LONG_FUNCTION",
"SpreadOperator"
)
override fun get(): RuleSet {
log.debug("Will run $DIKTAT_RULE_SET_ID with $diktatConfigFile" +
Expand Down Expand Up @@ -257,6 +258,8 @@ class DiktatRuleSetProvider(private var diktatConfigFile: String = DIKTAT_ANALYS
private fun resolveConfigFileFromSystemProperty(): String? = System.getProperty(DIKTAT_CONF_PROPERTY)

companion object {
private val log = LoggerFactory.getLogger(DiktatRuleSetProvider::class.java)
private val log = KotlinLogging.logger(
LoggerFactory.getLogger(DiktatRuleSetProvider::class.java)
).initKtLintKLogger()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
import com.pinterest.ktlint.core.ast.children
import com.pinterest.ktlint.core.ast.isLeaf
import com.pinterest.ktlint.core.initKtLintKLogger
import mu.KotlinLogging
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
Expand Down Expand Up @@ -281,7 +283,9 @@ class PackageNaming(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
private val log = LoggerFactory.getLogger(PackageNaming::class.java)
private val log = KotlinLogging.logger(
LoggerFactory.getLogger(PackageNaming::class.java)
).initKtLintKLogger()
const val NAME_ID = "package-naming"

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT
import com.pinterest.ktlint.core.ast.ElementType.FILE
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
import com.pinterest.ktlint.core.ast.prevSibling
import com.pinterest.ktlint.core.initKtLintKLogger
import mu.KotlinLogging
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.TokenType
import org.jetbrains.kotlin.lexer.KtTokens
Expand Down Expand Up @@ -173,7 +175,9 @@ class CommentsRule(configRules: List<RulesConfig>) : DiktatRule(

@Suppress("MaxLineLength")
companion object {
private val logger = LoggerFactory.getLogger(CommentsRule::class.java)
private val logger = KotlinLogging.logger(
LoggerFactory.getLogger(CommentsRule::class.java)
).initKtLintKLogger()
const val NAME_ID = "comments"
private val importKeywordWithSpace = "${KtTokens.IMPORT_KEYWORD.value} "
private val packageKeywordWithSpace = "${KtTokens.PACKAGE_KEYWORD.value} "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import com.pinterest.ktlint.core.ast.ElementType.KDOC
import com.pinterest.ktlint.core.ast.ElementType.PACKAGE_DIRECTIVE
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
import com.pinterest.ktlint.core.ast.isWhiteSpace
import com.pinterest.ktlint.core.initKtLintKLogger
import mu.KotlinLogging
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafElement
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
Expand Down Expand Up @@ -273,7 +275,9 @@ class HeaderCommentRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
private val log = LoggerFactory.getLogger(HeaderCommentRule::class.java)
private val log = KotlinLogging.logger(
LoggerFactory.getLogger(HeaderCommentRule::class.java)
).initKtLintKLogger()
const val CURR_YEAR_PATTERN = ";@currYear;"
const val NAME_ID = "header-comment"
val hyphenRegex = Regex("""\d+-\d+""")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class RangeConventionalRule(configRules: List<RulesConfig>) : DiktatRule(
*/
class RangeConventionalConfiguration(config: Map<String, String>) : RuleConfiguration(config) {
/**
* Does ignore rangeTo function
* If true, don't suggest to replace `rangeTo` function with operator `..`
*/
val isRangeToIgnore = config["isRangeToIgnore"]?.toBoolean() ?: false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import com.pinterest.ktlint.core.ast.ElementType.WHEN_ENTRY
import com.pinterest.ktlint.core.ast.children
import com.pinterest.ktlint.core.ast.isPartOfComment
import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline
import com.pinterest.ktlint.core.initKtLintKLogger
import mu.KotlinLogging
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.psi.psiUtil.siblings
Expand Down Expand Up @@ -133,7 +135,9 @@ class TrailingCommaRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
private val log = LoggerFactory.getLogger(TrailingCommaRule::class.java)
private val log = KotlinLogging.logger(
LoggerFactory.getLogger(TrailingCommaRule::class.java)
).initKtLintKLogger()
const val NAME_ID = "trailing-comma"
val ktVersion = KotlinVersion(1, 4)
val whenChildrenTypes = listOf(WHEN_CONDITION_WITH_EXPRESSION, WHEN_CONDITION_IS_PATTERN, WHEN_CONDITION_IN_RANGE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ import com.pinterest.ktlint.core.ast.ElementType.VALUE_PARAMETER_LIST
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline
import com.pinterest.ktlint.core.ast.visit
import com.pinterest.ktlint.core.initKtLintKLogger
import mu.KotlinLogging
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
Expand Down Expand Up @@ -550,7 +552,9 @@ class IndentationRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
private val log = LoggerFactory.getLogger(IndentationRule::class.java)
private val log = KotlinLogging.logger(
LoggerFactory.getLogger(IndentationRule::class.java)
).initKtLintKLogger()
const val NAME_ID = "indentation"
private val increasingTokens: Set<IElementType> = linkedSetOf(LPAR, LBRACE, LBRACKET, LONG_TEMPLATE_ENTRY_START)
private val decreasingTokens: Set<IElementType> = linkedSetOf(RPAR, RBRACE, RBRACKET, LONG_TEMPLATE_ENTRY_END)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline
import com.pinterest.ktlint.core.ast.nextCodeSibling
import com.pinterest.ktlint.core.ast.parent
import com.pinterest.ktlint.core.ast.prevCodeSibling
import com.pinterest.ktlint.core.initKtLintKLogger
import mu.KotlinLogging
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
Expand Down Expand Up @@ -710,7 +712,9 @@ class NewlinesRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
private val log = LoggerFactory.getLogger(NewlinesRule::class.java)
private val log = KotlinLogging.logger(
LoggerFactory.getLogger(NewlinesRule::class.java)
).initKtLintKLogger()
const val MAX_CALLS_IN_ONE_LINE = 3
const val NAME_ID = "newlines"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_INDENTATION
import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_WHITESPACE
import org.cqfn.diktat.ruleset.rules.DiktatRule
import org.cqfn.diktat.ruleset.rules.chapter3.LineLength
import org.cqfn.diktat.ruleset.rules.chapter6.classes.CompactInitialization
import org.cqfn.diktat.ruleset.utils.appendNewlineMergingWhiteSpace
import org.cqfn.diktat.ruleset.utils.calculateLineColByOffset
import org.cqfn.diktat.ruleset.utils.findParentNodeWithSpecificType
Expand Down Expand Up @@ -76,6 +75,8 @@ import com.pinterest.ktlint.core.ast.isWhiteSpace
import com.pinterest.ktlint.core.ast.nextCodeLeaf
import com.pinterest.ktlint.core.ast.parent
import com.pinterest.ktlint.core.ast.prevSibling
import com.pinterest.ktlint.core.initKtLintKLogger
import mu.KotlinLogging
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafElement
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
Expand Down Expand Up @@ -456,7 +457,9 @@ class WhiteSpaceRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
private val log = LoggerFactory.getLogger(CompactInitialization::class.java)
private val log = KotlinLogging.logger(
LoggerFactory.getLogger(WhiteSpaceRule::class.java)
).initKtLintKLogger()
const val NAME_ID = "horizontal-whitespace"

private const val NUM_PARENTS_FOR_LAMBDA = 3 // this is the number of parent nodes needed to check if this node is lambda from argument list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.THIS_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
import com.pinterest.ktlint.core.ast.isPartOfComment
import com.pinterest.ktlint.core.initKtLintKLogger
import mu.KotlinLogging
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace
import org.jetbrains.kotlin.psi.KtBinaryExpression
Expand Down Expand Up @@ -211,7 +213,9 @@ class CompactInitialization(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
private val log = LoggerFactory.getLogger(CompactInitialization::class.java)
private val log = KotlinLogging.logger(
LoggerFactory.getLogger(CompactInitialization::class.java)
).initKtLintKLogger()
const val NAME_ID = "class-compact-initialization"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
@file:Suppress(
"FILE_NAME_MATCH_CLASS",
"KDOC_WITHOUT_RETURN_TAG",
"KDOC_WITHOUT_PARAM_TAG"
"KDOC_WITHOUT_PARAM_TAG",
"MatchingDeclarationName",
)

package org.cqfn.diktat.ruleset.utils
Expand Down Expand Up @@ -61,16 +62,9 @@ import org.jetbrains.kotlin.psi.KtParameterList
import org.jetbrains.kotlin.psi.psiUtil.children
import org.jetbrains.kotlin.psi.psiUtil.parents
import org.jetbrains.kotlin.psi.psiUtil.siblings
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import java.util.Locale

/**
* A [Logger] that can be used throughout diktat
*/
private val log: Logger = LoggerFactory.getLogger("AstNodeUtils")

/**
* A class that represents result of nodes swapping. [oldNodes] should always have same size as [newNodes]
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
package org.cqfn.diktat.ruleset.utils

import com.google.common.base.CaseFormat
import com.pinterest.ktlint.core.initKtLintKLogger
import mu.KotlinLogging
import org.slf4j.LoggerFactory

import java.util.Locale

private val log = LoggerFactory.getLogger("StringCaseUtils")
private val log = KotlinLogging.logger(
LoggerFactory.getLogger("StringCaseUtils")
).initKtLintKLogger()

/**
* Available cases to name enum members
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.RuleSetProvider
import com.pinterest.ktlint.core.api.FeatureInAlphaState
import com.pinterest.ktlint.core.initKtLintKLogger
import mu.KotlinLogging
import org.assertj.core.api.Assertions
import org.assertj.core.api.SoftAssertions
import org.intellij.lang.annotations.Language
Expand All @@ -25,7 +27,9 @@ import java.util.function.Consumer
internal const val TEST_FILE_NAME = "TestFileName.kt"

@Suppress("WRONG_WHITESPACE")
private val log = LoggerFactory.getLogger({}.javaClass)
private val log = KotlinLogging.logger(
petertrr marked this conversation as resolved.
Show resolved Hide resolved
LoggerFactory.getLogger({}.javaClass)
).initKtLintKLogger()

@Suppress("TYPE_ALIAS")
internal val defaultCallback: (lintError: LintError, corrected: Boolean) -> Unit = { lintError, _ ->
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@
<artifactId>commons-configuration</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>io.github.microutils</groupId>
<artifactId>kotlin-logging-jvm</artifactId>
<version>2.1.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down