pinterest / ktlint

An anti-bikeshedding Kotlin linter with built-in formatter
https://pinterest.github.io/ktlint/
MIT License
6.23k stars 507 forks source link

Please use slf4j logger and level instead of logback-classic directly #2807

Closed Vampire closed 1 month ago

Vampire commented 1 month ago

If you try to use ktlint as a library to do some formatting after code-generation, there is a problem because of the direct usage of logback-classic classes Logger and Level.

If you have no conflicting stuff on your classpath, the problem might just be, that the log messages are not consolidated in one logging framework. In other cases, like when using ktlint as part of a Gradle plugin, you even get errors like:

java.lang.ClassCastException: class org.gradle.internal.logging.slf4j.OutputEventListenerBackedLogger cannot be cast to class ch.qos.logback.classic.Logger (org.gradle.internal.logging.slf4j.OutputEventListenerBackedLogger is in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @64b8f8f4; ch.qos.logback.classic.Logger is in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader$InstrumentingVisitableURLClassLoader @27256089)
    at com.pinterest.ktlint.cli.internal.KtlintCommandLine.underlyingLogger(KtlintCommandLine.kt:389)

Please instead use the slf4j classes instead which are the separate API for logback.

This way one can depend on ktlint-cli and exclude the logback dependency, handling the slf4j with whatever log framework is applicable.

Even better would of course additionally be to not have a dependency on logback classic at all in the published library on MavenCentral and only add it to distributions for standalone usage, so that the published library can directly be used as library.

Vampire commented 1 month ago

Ah, sorry, just have seen that ktlint-rule-engine is the artifact to use for integrations.