【问题标题】:class path contains multiple SLF4J bindings spring boot exclude issue类路径包含多个 SLF4J 绑定 spring boot 排除问题
【发布时间】:2018-02-12 12:02:17
【问题描述】:

我试图在我的 springboot 项目中包含 log4j2,但出现以下错误。

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/mn/.gradle/caches/modules-2/files-2.1/org.apache.logging.log4j/log4j-slf4j-impl/2.10.0/8e4e0a30736175e31c7f714d95032c1734cfbdea/log4j-slf4j-impl-2.10.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/mn/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.2.3/7c4f3c474fb2c041d8028740440937705ebb473a/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]

我的 gradle 文件如下所示:

dependencies {                                                                      
  compile('org.springframework.boot:spring-boot-starter') {                         
    exclude module:'spring-boot-starter-logging'                                    
  }                                                                                 
  compile('org.springframework.boot:spring-boot-starter-web')                       
  compile('org.springframework.boot:spring-boot-starter-security')                  
  compile('org.springframework.boot:spring-boot-starter-log4j2')                    
  compile('org.springframework:spring-oxm')                                         
  compile('org.codehaus.castor:castor-xml:1.3.3')                                   
  compile('org.apache.commons:commons-collections4:4.1')                            

  testCompile('org.springframework.boot:spring-boot-starter-test')   
  testCompile('org.springframework.security:spring-security-test')                  
} 

我很确定 Spring-boot-starter 是实现日志记录的那个,这就是为什么我尝试排除它,但它不起作用。我排除它是错误的,还是错误的事情?

【问题讨论】:

  • 对于初学者,请停止混合 Spring Boot 版本、Spring 版本和 Spring Security 版本。 Spring Boot 1.5 不适用于 Spring Security 5(从 spring-security-test 中删除版本,这也适用于 spring-boot-start-test、spring-boot-starter-log4j2` 和 spring-oxm 依赖项)。
  • 你想让我从所有这些中删除版本,还是只删除 spring-security-test?
  • 所有这些,Spring Boot 将为您管理版本。您目前正在混合至少 2 个 spring 版本(可能是 3 个)以及不同的依赖项和托管依赖项。
  • 编辑了 gradle 文件。现在我在哪里声明要使用的实际版本?
  • 你并没有像所说的那样,通过 Spring Boot 插件为你管理 Spring Boot。

标签: spring spring-boot logging gradle log4j2


【解决方案1】:

解决了这个问题。这是因为 spring-boot-starter-web 和 spring-boot-starter-security 也使用标准日志记录,因此必须将其排除在所有日志之外

【讨论】:

    【解决方案2】:

    我用过这个

    configurations {
        //eliminates logback
        all*.exclude group: 'ch.qos.logback'
    
        //eliminates StackOverflowError
        all*.exclude group: 'org.apache.logging.log4j', module: 'log4j-to-slf4j'
    }
    

    【讨论】:

      【解决方案3】:

      spring-boot-starter 中的多个类使用spring-boot-starter-logging:

      • 就我而言,spring-boot-starter-jdbcspring-boot-starter-web

      你应该使用<exclusion>标签排除依赖,像这样:

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

      【讨论】:

        猜你喜欢
        • 2020-07-07
        • 2012-12-11
        • 2016-03-12
        • 2014-05-18
        • 2012-09-11
        • 2017-02-27
        • 2021-05-19
        • 2019-08-01
        相关资源
        最近更新 更多