Skip to content

Commit

Permalink
Use ktlint's mechanism to initialize loggers (#1414)
Browse files Browse the repository at this point in the history
### What's done:
* Initialize loggers using kotlin-logging and extension from ktlint-core

This pull request closes #1413. Full description of the problem is in #1394
  • Loading branch information
petertrr authored Jun 30, 2022
1 parent 1445b6e commit 43e6098
Show file tree
Hide file tree
Showing 21 changed files with 97 additions and 43 deletions.
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,6 +1,7 @@
package org.cqfn.diktat.common.config.reader

import org.slf4j.LoggerFactory
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import mu.KotlinLogging

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

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

import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import mu.KotlinLogging
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import java.io.BufferedReader
import java.io.IOException
Expand Down Expand Up @@ -60,6 +61,6 @@ abstract class JsonResourceConfigReader<T> {
/**
* A [Logger] that can be used
*/
val log: Logger = LoggerFactory.getLogger(JsonResourceConfigReader::class.java)
val log: Logger = KotlinLogging.loggerWithKtlintConfig(JsonResourceConfigReader::class)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ package org.cqfn.diktat.common.config.rules

import org.cqfn.diktat.common.config.reader.JsonResourceConfigReader
import org.cqfn.diktat.common.config.rules.RulesConfigReader.Companion.log
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig

import com.charleskorn.kaml.Yaml
import com.charleskorn.kaml.YamlConfiguration
import mu.KotlinLogging
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import java.io.BufferedReader
import java.io.File
Expand Down Expand Up @@ -110,7 +111,7 @@ 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.loggerWithKtlintConfig(RulesConfigReader::class)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Utilities related to logging
*/

package org.cqfn.diktat.common.utils

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

/**
* Create a logger using [KotlinLogging] and configure it by ktlint's mechanism
*
* @param func empty fun which is used to get enclosing class name
* @return a logger
*/
fun KotlinLogging.loggerWithKtlintConfig(func: () -> Unit) =
logger(func).initKtLintKLogger()

/**
* Create a logger using [KotlinLogging] and configure it by ktlint's mechanism
*
* @param clazz a class for which logger is needed
* @return a logger
*/
fun KotlinLogging.loggerWithKtlintConfig(clazz: KClass<*>) =
logger(LoggerFactory.getLogger(clazz.java)).initKtLintKLogger()
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 @@ -2,12 +2,13 @@ package org.cqfn.diktat.ruleset.rules

import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.isRuleEnabled
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.ruleset.constants.EmitType
import org.cqfn.diktat.ruleset.utils.getFilePath

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

private typealias DiktatConfigRule = org.cqfn.diktat.common.config.rules.Rule

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

companion object {
private val log = LoggerFactory.getLogger(DiktatRule::class.java)
private val log = KotlinLogging.loggerWithKtlintConfig(DiktatRule::class)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.cqfn.diktat.common.config.rules.DIKTAT_CONF_PROPERTY
import org.cqfn.diktat.common.config.rules.DIKTAT_RULE_SET_ID
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.RulesConfigReader
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.ruleset.constants.Warnings
import org.cqfn.diktat.ruleset.rules.OrderedRuleSet.Companion.ordered
import org.cqfn.diktat.ruleset.rules.chapter1.FileNaming
Expand Down Expand Up @@ -85,14 +86,14 @@ import org.cqfn.diktat.ruleset.rules.chapter6.classes.StatelessClassesRule

import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.RuleSetProvider
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 +108,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 +257,6 @@ 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.loggerWithKtlintConfig(DiktatRuleSetProvider::class)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.cqfn.diktat.ruleset.rules.chapter1
import org.cqfn.diktat.common.config.rules.CommonConfiguration
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.getCommonConfiguration
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.ruleset.constants.Warnings.INCORRECT_PACKAGE_SEPARATOR
import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_CASE
import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_PATH
Expand All @@ -24,12 +25,12 @@ 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 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
import org.jetbrains.kotlin.konan.file.File
import org.jetbrains.kotlin.lexer.KtTokens.PACKAGE_KEYWORD
import org.slf4j.LoggerFactory

import java.util.concurrent.atomic.AtomicInteger

Expand Down Expand Up @@ -281,7 +282,7 @@ class PackageNaming(configRules: List<RulesConfig>) : DiktatRule(
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cqfn.diktat.ruleset.rules.chapter2.comments

import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.ruleset.constants.ListOfPairs
import org.cqfn.diktat.ruleset.constants.Warnings.COMMENTED_OUT_CODE
import org.cqfn.diktat.ruleset.rules.DiktatRule
Expand All @@ -12,13 +13,13 @@ 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 mu.KotlinLogging
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.TokenType
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.resolve.ImportPath
import org.slf4j.LoggerFactory

/**
* This rule performs checks if there is any commented code.
Expand Down Expand Up @@ -173,7 +174,7 @@ class CommentsRule(configRules: List<RulesConfig>) : DiktatRule(

@Suppress("MaxLineLength")
companion object {
private val logger = LoggerFactory.getLogger(CommentsRule::class.java)
private val logger = KotlinLogging.loggerWithKtlintConfig(CommentsRule::class)
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 @@ -3,6 +3,7 @@ package org.cqfn.diktat.ruleset.rules.chapter2.comments
import org.cqfn.diktat.common.config.rules.RuleConfiguration
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.getRuleConfig
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.ruleset.constants.Warnings.HEADER_MISSING_IN_NON_SINGLE_CLASS_FILE
import org.cqfn.diktat.ruleset.constants.Warnings.HEADER_MISSING_OR_WRONG_COPYRIGHT
import org.cqfn.diktat.ruleset.constants.Warnings.HEADER_NOT_BEFORE_PACKAGE
Expand All @@ -26,11 +27,11 @@ 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 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
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
import org.slf4j.LoggerFactory

import java.time.LocalDate

Expand Down Expand Up @@ -273,7 +274,7 @@ class HeaderCommentRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
private val log = LoggerFactory.getLogger(HeaderCommentRule::class.java)
private val log = KotlinLogging.loggerWithKtlintConfig(HeaderCommentRule::class)
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 @@ -4,6 +4,7 @@ import org.cqfn.diktat.common.config.rules.RuleConfiguration
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.getCommonConfiguration
import org.cqfn.diktat.common.config.rules.getRuleConfig
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.ruleset.constants.Warnings.TRAILING_COMMA
import org.cqfn.diktat.ruleset.rules.DiktatRule

Expand Down Expand Up @@ -32,10 +33,10 @@ 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 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
import org.slf4j.LoggerFactory

/**
* [1] Enumerations (In another rule)
Expand Down Expand Up @@ -133,7 +134,7 @@ class TrailingCommaRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
private val log = LoggerFactory.getLogger(TrailingCommaRule::class.java)
private val log = KotlinLogging.loggerWithKtlintConfig(TrailingCommaRule::class)
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 @@ -6,6 +6,7 @@ package org.cqfn.diktat.ruleset.rules.chapter3.files

import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.getRuleConfig
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_INDENTATION
import org.cqfn.diktat.ruleset.rules.DiktatRule
import org.cqfn.diktat.ruleset.utils.NEWLINE
Expand Down Expand Up @@ -62,6 +63,7 @@ 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 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 All @@ -75,7 +77,6 @@ import org.jetbrains.kotlin.psi.KtLoopExpression
import org.jetbrains.kotlin.psi.psiUtil.parents
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.slf4j.LoggerFactory

import kotlin.math.abs
import kotlin.reflect.KCallable
Expand Down Expand Up @@ -550,7 +551,7 @@ class IndentationRule(configRules: List<RulesConfig>) : DiktatRule(
}

companion object {
private val log = LoggerFactory.getLogger(IndentationRule::class.java)
private val log = KotlinLogging.loggerWithKtlintConfig(IndentationRule::class)
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 @@ -3,6 +3,7 @@ package org.cqfn.diktat.ruleset.rules.chapter3.files
import org.cqfn.diktat.common.config.rules.RuleConfiguration
import org.cqfn.diktat.common.config.rules.RulesConfig
import org.cqfn.diktat.common.config.rules.getRuleConfig
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import org.cqfn.diktat.ruleset.constants.ListOfList
import org.cqfn.diktat.ruleset.constants.Warnings.COMPLEX_EXPRESSION
import org.cqfn.diktat.ruleset.constants.Warnings.REDUNDANT_SEMICOLON
Expand Down Expand Up @@ -86,6 +87,7 @@ 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 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 All @@ -102,7 +104,6 @@ import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
import org.jetbrains.kotlin.psi.psiUtil.children
import org.jetbrains.kotlin.psi.psiUtil.parents
import org.jetbrains.kotlin.psi.psiUtil.siblings
import org.slf4j.LoggerFactory

/**
* Rule that checks line break styles.
Expand Down Expand Up @@ -710,7 +711,7 @@ class NewlinesRule(configRules: List<RulesConfig>) : DiktatRule(
}

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

Expand Down
Loading

0 comments on commit 43e6098

Please sign in to comment.