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.
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
andLevel
.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:
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.