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

当前回答

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

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

其他回答

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")
)

<scope>提供</scope>和< exclsions >的组合不适合我。

我不得不使用这个:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <scope>system</scope>
    <systemPath>${project.basedir}/empty.jar</systemPath>
</dependency>

其中empty.jar是一个没有任何内容的jar文件。

在我的情况下,我有两个来源的依赖log4 1在C:\Program Files\smcf.Ear目录和来自maven的第二个目录导致了sl4j的多重绑定。

删除smcf。耳朵目录为我解决了这个问题。

我只是忽略/删除了那个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'
}