spring_reference/IX. ‘How-to’ guides/66.2. Configure Log4j for l...

29 lines
1.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

### 66.2. 配置Log4j
Spring Boot也支持[Log4j](http://logging.apache.org/log4j/1.2)或[Log4j 2](http://logging.apache.org/log4j/2.x)作为日志配置但只有在它们中的某个在classpath下存在的情况。如果你正在使用starter poms进行依赖装配这意味着你需要排除Logback然后包含你选择的Log4j版本。如果你不使用starter poms那除了你选择的Log4j版本外还要提供commons-logging至少
最简单的方式可能就是通过starter poms尽管它需要排除一些依赖比如在Maven中
```xml
<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-log4j</artifactId>
</dependency>
```
想要使用Log4j 2只需要依赖`spring-boot-starter-log4j2`而不是`spring-boot-starter-log4j`。
**注**使用Log4j各版本的starters都会收集好依赖以满足common logging的要求比如Tomcat中使用`java.util.logging`但使用Log4j或 Log4j 2作为输出。具体查看Actuator Log4j或Log4j 2的示例了解如何将它用于实战。