我得到如下错误。似乎有多个日志框架绑定到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.

当前回答

我也有同样的问题。在我的pom.xml中,两者都有

 <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.28</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.2.1.RELEASE</version>
    </dependency>

当我删除spring-boot-starter-web依赖时,问题就解决了。

其他回答

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

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'
}

对于那些寻找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

这个问题是因为StaticLoggerBinder.class属于两个不同的jar。这个类引用自logback-classic-1.2.3.jar,同样的类也引用自log4j-slf4j- pil -2.10.0.jar。两个jar都在类路径中。因此他们之间产生了冲突。 这是log4j2.xml文件在类路径[src/main/resource]下没有生成日志文件的原因。

我们已经选择了一个jar,我建议使用log4j-slf4j- pple -2.10.0.jar文件,排除logback-classic-1.2.3.jar文件。 解决方法:打开pom文件,查看依赖层次结构[eclipse]或运行 MVN dependency:tree命令用于查找下载依赖的依赖树和依赖源。找到冲突的依赖项并排除它们。对于Springboot应用程序,请尝试这样做。

<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-actuator</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

This is working fine for me after struggling a lots.

我通过Intellij项目中的Project Structure解决了这个问题。 我删除了名为:Maven: org.apache.logging.log4j:log4j-to-slf4j-impl:2.14.1的文件

这个文件没有显示在这张图片中。您可能会看到两个库被称为log4j-to-slf4j。删除一个就可以了。