为什么我们需要在 Spring Boot 中为 log42j 添加额外的依赖?

Gau*_*hal 5 maven log4j2 spring-boot

既然所有传递依赖项都已经存在于 spring-boot-starter-web 中,为什么我们需要为 log42j 添加额外的依赖项?从我可以在互联网上阅读的任何教程中,每个人都提到添加依赖项:

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

他们还提到spring-boot-starter-logging排除spring-boot-starter-web

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

但为什么我们需要这样做呢?我看到 中的所有依赖项spring-boot-starter-log4j2都已存在于 中spring-boot-starter-logging

Ank*_*pta 0

日志记录的默认依赖项不是 log4j,而是 logback。如果删除依赖项,理想情况下您应该无法在项目中使用 log4j 导入。你能检查一次吗?log4j 依赖项仅在依赖项管理中声明,但您需要包含该依赖项才能使用此声明的依赖项。

参考:https ://docs.spring.io/spring-boot/docs/2.0.x/reference/html/howto-logging.html