解决lsf4j多次引用的问题

基于Spring开发过程中,时常遇到类似:SLF4J: Class path contains multiple SLF4J bindings.的warning,不影响使用,但也可以解决。

1、首先要准确定位出错的位置:

参考报错信息:

SLF4J: Found binding in [jar:file:/Users/zhao/.m2/repository/ch/qos/logback/logback-classic/1.2.11/logback-classic-1.2.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/zhao/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.17.2/log4j-slf4j-impl-2.17.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]

我这里是提示一个与logback相关,一个与log4j-slf4j相关

接下来,使用mvn工具协助分析,确定报错位置:

mvn dependency:tree
[INFO] com.grid:heat-search:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-data-elasticsearch:jar:2.7.4:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.7.4:compile
[INFO] |  |  +- org.springframework.boot:spring-boot:jar:2.7.4:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.7.4:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.7.4:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.2.11:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.2.11:compile
[INFO] |  |  |  \- org.apache.logging.log4j:log4j-to-slf4j:jar:2.17.2:compile
[INFO] |  |  +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.30:compile
[INFO] |  \- org.springframework.data:spring-data-elasticsearch:jar:4.4.3:compile
[INFO] |     \- org.slf4j:slf4j-api:jar:1.7.36:compile
[INFO] \- org.springframework.boot:spring-boot-starter-log4j2:jar:2.7.4:compile
[INFO]    +- org.apache.logging.log4j:log4j-slf4j-impl:jar:2.17.2:compile
[INFO]    +- org.apache.logging.log4j:log4j-core:jar:2.17.2:compile
[INFO]    +- org.apache.logging.log4j:log4j-jul:jar:2.17.2:compile
[INFO]    \- org.slf4j:jul-to-slf4j:jar:1.7.36:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  48.006 s
[INFO] Finished at: 2022-10-08T15:05:58+08:00
[INFO] ------------------------------------------------------------------------

在这里,仅保留了与报错相关的内容。

找到的相关内容有:

org.springframework.boot:spring-boot-starter包中的:ch.qos.logback:logback-classic 与 org.slf4j:slf4j-api

org.springframework.boot:spring-boot-starter-log4j2 中的

org.slf4j:jul-to-slf4j

结合前面的报错提示,可以确认,出错位置为:org.springframework.boot:spring-boot-starter包中的ch.qos.logback:logback-classic

2、解决问题

修改elasticsearch的依赖,如下:

        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch

            
                
                    ch.qos.logback
                    logback-classic
                
                
                    org.apache.logging.log4j
                    log4j-to-slf4j
                
            
        

把这两个包排除在外即可。

特殊说明:我先排除了logback-claasic包,编辑报错,又排除了log4j-to-slf4j,编译通过。

你可能感兴趣的:(Elasticsearch,log4j,多次引用,lsf4j)