有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java是由:org引起的。阿帕奇。登录中。log4j。LoggingException:log4jslf4jimpl不能与log4jtoslf4j一起出现

在我的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中:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration debug="true" xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="Console"
              class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <!-- l, L, M - is extremely slow. It's use should be avoided unless execution
                speed is not an issue. -->
            <param name="ConversionPattern"
                   value="%p: %d{dd.MM.yyyy HH:mm:ss.SSS} %l %n    %m%n"/>
        </layout>
    </appender>

    <appender name="File"
              class="org.apache.log4j.DailyRollingFileAppender">
        <param name="Encoding" value="UTF-8"/>
        <param name="File" value="logs/trace.log"/>
        <param name="Append" value="true"/>
        <layout class="org.apache.log4j.PatternLayout">
            <!-- l, L, M - is extremely slow. It's use should be avoided unless execution
                speed is not an issue. -->
            <param name="ConversionPattern"
                   value="%p: %d{dd.MM.yyyy HH:mm:ss.SSS} %l %n    %m%n"/>
        </layout>
    </appender>

    <!-- Application Loggers -->
    <logger name="com.journaldev.spring">
        <level value="info"/>
    </logger>

    <!-- 3rdparty Loggers -->
    <logger name="org.springframework.core">
        <level value="info"/>
    </logger>

    <logger name="org.hibernate">
        <level value="info"/>
    </logger>

    <logger name="org.springframework.beans">
        <level value="info"/>
    </logger>

    <logger name="org.springframework.context">
        <level value="info"/>
    </logger>

    <logger name="org.springframework.web">
        <level value="info"/>
    </logger>

    <!-- The root category is used for all loggers unless a more specific logger
        matches. If none of the loggers are assigned a level, then all loggers inherit
        the level of the root logger which is set to DEBUG by default -->
    <root>
        <level value="ALL"/>
        <appender-ref ref="Console"/>
        <!-- <appender-ref ref="File" /> -->
    </root>

</log4j:configuration>

在我的控制器中:

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


共 (3) 个答案

  1. # 1 楼答案

    根据Spring的documentation(正如Simon所指出的),我们决定将“Spring boot starter logging”模块从所有库中排除,而不仅仅是从“Spring boot starter web”中排除

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

    。。。而不是

    dependencies {
        ...
        implementation('org.springframework.boot:spring-boot-starter') {
            exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
        }
    }
    

    我也遇到了同样的问题,用这个解决方案解决了

  2. # 2 楼答案

    我从构建中排除了spring引导日志记录。但问题仍在发生。它通过从中删除org.apache.logging.log4j/log4j-slf4j-impl/2.12.1得到解决。类路径

  3. # 3 楼答案

    即使在纠正了类路径之后,我在STS中也遇到了同样的问题,Gradle的“全部刷新”解决了这个问题