> For the complete documentation index, see [llms.txt](https://jack80342.gitbook.io/spring-boot/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jack80342.gitbook.io/spring-boot/ix.-how-to-guides/78.-logging/78.2-configure-log4j-for-logging.md).

# 78.2 配置Log4j

如果[Log4j 2](https://logging.apache.org/log4j/2.x)出现在类路径下，Spring Boot会将其作为日志配置。如果你正在使用starter进行依赖装配，这意味着你需要排除Logback，然后包含log4j 2。如果不使用starters，除了添加Log4j 2，你还需要提供`spring-jcl`依赖（至少）。

最简单的方式可能就是通过starter，尽管它需要排除一些依赖，比如，在Maven中：

```markup
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
```

**注** Log4j starters会收集好依赖以满足普通日志记录的需求（比如，Tomcat中使用`java.util.logging`，但使用Log4j 2作为输出）。具体查看[Actuator Log4j 2](https://github.com/spring-projects/spring-boot/tree/v2.0.0.RELEASE/spring-boot-samples/spring-boot-sample-actuator-log4j2)的示例，了解如何将它用于实战。

**注** 通过设置`java.util.logging.manager`系统属性为`org.apache.logging.log4j.jul.LogManager`，来配置它的[JDK日志适配器](https://logging.apache.org/log4j/2.0/log4j-jul/index.html)，确保使用`java.util.logging`执行的调试日志被路由到Log4j 2。
