SLF4J: Class path contains multiple SLF4J bindings

springboot log4j2 报错SLF4J: Class path contains multiple SLF4J bindings,maven依赖重复冲突的解决办法

springboot 使用log4j保存日志,网上随便找了篇博文参考,配置依赖照写,启动结果报错: Class path contains multiple SLF4J bindings
具体报错如下:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/E:/maven_repository_new/org/apache/logging/log4j/log4j-slf4j-impl/2.11.2/log4j-slf4j-impl-2.11.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/E:/maven_repository_new/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
Exception in thread "main" java.lang.StackOverflowError
    at org.apache.logging.log4j.util.StackLocator.getCallerClass(StackLocator.java:108)
    at org.apache.logging.log4j.util.StackLocator.getCallerClass(StackLocator.java:121)
    at org.apache.logging.log4j.util.StackLocatorUtil.getCallerClass(StackLocatorUtil.java:55)
    at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:42)
    at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:46)
    at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)

报错字面意思 有多个 SLF4J 绑定,log4j-slf4j-impl-2.11.2.jar 和 logback-classic-1.2.3.jar里面重复绑定SLF4J ,再看了下那博文,有这么一段,如项目中有导入spring-boot-starter-web依赖包记得去掉spring自带的日志依赖spring-boot-starter-logging,如下:

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

按上面说法是 spring-boot-starter-web 自带日志依赖,那既然exclusion排除了日志依赖咋还有问题!

实际是 spring-boot-starter 自带日志依赖 而不是 spring-boot-starter-web,下面才是正确的

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

如何快速正确的解决类似重复依赖的问题。

1 右键pom.xml -> Maven -> Show Dependencies
image.png
2 搜索重复的jar包被哪些上层依赖

比如 这里要搜索的是 logback-classic-1.2.3.jar 在哪里被依赖
在上面打开的依赖关系图中 crtl + f 打开搜索,然后输入搜索内容 logback-classic,如下:
点击找到的logback-classic ,会看到如下依赖关系:


image.png

3 找到了需要排除的jar被谁用到

那就在pom.xml中用排除掉,
如下:

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

最后

其他类似的maven 重复依赖问题,可以按上面方法尝试去解决!

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容