由于org.apache.logging.log4j.LoggingException引起:log4j-slf4j-impl不能与log4j-to-slf4j同时存在。

9 浏览
0 Comments

由于org.apache.logging.log4j.LoggingException引起:log4j-slf4j-impl不能与log4j-to-slf4j同时存在。

在我的Spring Boot 2项目中:

在build.gradle中:

dependencies {
    implementation 'com.google.code.gson:gson:2.7'
    implementation 'com.h2database:h2'
    implementation 'javax.servlet:jstl:1.2'
    implementation 'org.springframework.boot:spring-boot-devtools'
    implementation('org.springframework.boot:spring-boot-starter') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-log4j2'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation "org.springframework.boot:spring-boot-starter-web"
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testImplementation 'org.junit.jupiter:junit-jupiter:5.5.2'
}

在src/resources/log4j2.xml中:




    
        
            
            
        
    
    
        
        
        
        
            
            
        
    
    
    
        
    
    
    
        
    
    
        
    
    
        
    
    
        
    
    
        
    
    
    
        
        
        
    

在我的控制器中:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class CategoryController {
    private CategoryRepository categoryRepository;
    private static Logger logger = LoggerFactory.getLogger(CategoryController.class);
    
    @GetMapping("/categories")
    public String getCategories(Model model) {
        logger.debug("getCategories>>>>>>>>>>>>>>>>");
        model.addAttribute("categoryList", this.categoryRepository.findAll());
        return "categories/category_list";
    }
}

但是当我启动项目时出现错误:

Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl
  cannot be present with log4j-to-slf4j

0
0 Comments

通过错误日志确定要排除哪个项目。

例如,对于像这样的错误消息:Caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j

我使用了Gradle的排除功能:

configurations.all { exclude group: 'org.apache.logging.log4j' }

原因:

该错误是由于在同一个项目中同时引入了log4j-slf4j-impl和log4j-to-slf4j两个依赖包导致的。由于它们是相互冲突的,所以会产生冲突错误。

解决方法:

我们可以通过在构建工具(如Gradle或Maven)的配置文件中排除其中一个依赖包来解决这个问题。在上面的示例中,我们使用了Gradle的排除功能来排除log4j-slf4j-impl依赖包。

这样做可以确保只有一个依赖包被引入项目,从而消除了冲突错误。通过排除冲突的依赖包,我们可以避免由于它们之间的冲突而导致的错误,保证项目的正常运行。

0
0 Comments

问题原因:在使用Spring Boot 2.3.0.RELEASE版本时,如果classpath中存在log4j-slf4j-impl依赖,就会导致org.apache.logging.log4j.LoggingException异常的出现。

解决方法:可以通过移除其他log4j依赖来解决该问题。如果使用了starters来组装依赖,需要排除Logback,并替换为log4j 2。

在Gradle中可以这样做:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-log4j2'
}
configurations {
    all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
}

在Maven中可以这样做:


    org.springframework.boot
    spring-boot-starter
    
        
            org.springframework.boot
            spring-boot-starter-logging
        
    


    org.springframework.boot
    spring-boot-starter-log4j2

更多信息请参考官方文档:[https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-log4j-for-logging](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-log4j-for-logging)

0
0 Comments

根据Spring文档的说明,我们需要从所有库中排除"spring-boot-starter-logging"模块,而不仅仅是从"spring-boot-starter-web"中排除。解决方法如下:

在Gradle中:

configurations {
    ...
    all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
}

如果使用Kotlin DSL:

configurations {

all {

exclude(group = "org.springframework.boot", module = "spring-boot-starter-logging")

}

}

如果使用Maven,可以使用以下方式实现相同的效果:


    org.springframework.boot
    spring-boot-starter
    
        
            org.springframework.boot
            spring-boot-starter-logging
        
    

以上是解决问题的方法,根据Spring文档的说明,我们需要从所有库中排除"spring-boot-starter-logging"模块,而不仅仅是从"spring-boot-starter-web"中排除。如果使用Gradle,可以使用上述的配置方式,在build.gradle文件中添加相应的代码。如果使用Kotlin DSL,则需要使用相应的语法。如果使用Maven,则需要在pom.xml文件中添加相应的依赖排除配置。

0