引起:org.apache.logging.log4j.LoggingException:log4j-slf4j-impl 不能与 log4j-to-slf4j 一起出现

a_s*_*ber 26 java spring log4j2 spring-boot

在我的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'
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

在我的控制器中:

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";
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我开始项目时,我收到错误:

引起:org.apache.logging.log4j.LoggingException:log4j-slf4j-impl 不能与 log4j-to-slf4j 一起出现

小智 50

根据 Spring 的文档(正如 Simon 所指出的),我们希望从所有库中排除“spring-boot-starter-logging”模块,而不仅仅是从“spring-boot-starter-web”中排除。

configurations {
    ...
    all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
}
Run Code Online (Sandbox Code Playgroud)

...代替...

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

只是让我自己遇到了同样的问题并用这个解决方案解决了。

  • 如果我使用 Maven 而不是 Gradle,如何执行上述操作(从所有库中排除)?您能提供与上述内容相当的 Maven 吗? (3认同)

veb*_*ben 17

Spring boot2.3.0.RELEASE版本,原生支持Log4j2,用于日志配置(如果它在类路径上)。在这种情况下,您可以简单地删除其他 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'
    }
}
Run Code Online (Sandbox Code Playgroud)

或者使用Maven

<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>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

更多官方文档信息:https : //docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-log4j-for-logging