我得到如下错误。似乎有多个日志框架绑定到slf4j。不知道如何解决这个问题。任何帮助都非常感激。

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/admin/.m2/repository/org/slf4j/slf4j-log4j12/1.6.4/slf4j-log4j12-1.6.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/admin/.m2/repository/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

当前回答

如果这些日志是此修复的结果: https://stackoverflow.com/a/9919375/2894819

当你的某个库实际使用它时。而且您的应用程序不需要SL4J,只需将实现替换为runtimeOnly即可。

// contains dependency to sl4j-api
implementation("com.github.doyaaaaaken:kotlin-csv-jvm:1.2.0")

// add this to remove both warnings
runtimeOnly("org.slf4j:slf4j-nop:1.7.36")

在这种情况下,当你运行你的应用程序时,实际的依赖关系将被库包含一次,而不会被包含到你的application.jar本身的bundle中。

其他回答

我在一个非maven项目中得到了这个问题,两个依赖的jar每个都包含一个slf4j。我解决了 通过删除一个依赖的jar,编译项目(当然会失败),然后将删除的jar添加回来。

Sbt版:

添加排除(“org。Slf4j ", " Slf4j -log4j12")传递到包含Slf4j -log4j12的依赖项。例如,当使用Log4j 2.6使用Spark时:

libraryDependencies ++= Seq(
  // One SLF4J implementation (log4j-slf4j-impl) is here:
  "org.apache.logging.log4j" % "log4j-api" % "2.6.1",
  "org.apache.logging.log4j" % "log4j-core" % "2.6.1",
  "org.apache.logging.log4j" % "log4j-slf4j-impl" % "2.6.1",
  // The other implementation (slf4j-log4j12) would be transitively
  // included by Spark. Prevent that with exclude().
  "org.apache.spark" %% "spark-core" % "1.5.1" exclude("org.slf4j", "slf4j-log4j12")
)

1.找到矛盾的罐子

如果无法从警告中识别依赖项,那么可以使用以下命令来识别冲突的jar

mvn dependency: tree

这将显示项目的依赖关系树,以及与slf4j-log4j12 JAR导入另一个绑定的依赖关系。

决议

现在我们已经知道了有问题的依赖项,我们所需要做的就是从该依赖项中排除slf4j-log4j12 JAR。

Ex -如果spring-安全依赖也拉入了slf4j-log4j12 JAR的另一个绑定,那么我们需要从spring-安全依赖中排除slf4j-log4j12 JAR。

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
           <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
    </dependency>

注意:在某些情况下,已经有多个依赖项拉入了slf4j-log4j12 JAR的绑定,您不需要为每个拉入的依赖项添加exclude。 你只需要在最初放置的依赖项上添加排除依赖项。

Ex -

<dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

</dependencies>

如果你使用gradle,那么添加以下代码到你的构建中。gradle文件从所有模块中排除SLF4J绑定

configurations.all {
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}

只使用所需的依赖项,而不是全部:)))。对于我来说,对于正常的日志记录过程,您需要这个依赖项来排除pom.xml中的其他依赖项

<dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.8</version>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.1.8</version>
    </dependency>

对于那些寻找spring-boot类型依赖的解决方案的人来说,Gradle的魔法咒语是这样的:

configurations.all {
    exclude group: 'ch.qos.logback', module: 'logback-classic'
}

在你的构建中。Gradle在顶层(不是在依赖块内)。

在网上找到的所有其他解决方案(包括这里建议排除slf4j模块的解决方案)对我都不起作用。

这就是我的身材。gradle(片段):


// Removes the annoying warning about the multiple SLF4J implementations:
//    SLF4J: Class path contains multiple SLF4J bindings.
configurations.all {
    exclude group: 'ch.qos.logback', module: 'logback-classic'
}

dependencies {
    annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"

    implementation ('org.springframework.boot:spring-boot-starter-actuator')
    implementation ('org.springframework.boot:spring-boot-starter-data-mongodb-reactive')
    implementation ('org.springframework.boot:spring-boot-starter-webflux')

    annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
    compileOnly "org.projectlombok:lombok:${lombokVersion}"

    // Removes the annoying warning:
    // warning: unknown enum constant When.MAYBE
    //  reason: class file for javax.annotation.meta.When not found
    // See: https://stackoverflow.com/questions/29805622/could-not-find-or-load-main-class-org-gradle-wrapper-gradlewrappermain/31622432
    implementation group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.2'

    // other stuff...

YMMV