springboot导入log4j依赖
时间: 2023-10-12 20:13:45 浏览: 98
SpringBoot整合log4j
你可以在Spring Boot项目中使用Log4j作为日志框架,以下是在Maven项目中导入Log4j的步骤:
1. 在 pom.xml 文件中添加以下依赖:
```
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.0</version>
</dependency>
```
2. 添加 log4j2.xml 配置文件到 src/main/resources 目录下,例如:
```
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="com.example" level="DEBUG" additivity="false">
<AppenderRef ref="Console" />
</Logger>
<Root level="INFO">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
```
3. 在代码中使用 Log4j 记录日志,例如:
```
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class MyService {
private static final Logger logger = LogManager.getLogger(MyService.class);
public void doSomething() {
logger.info("Doing something...");
// ...
}
}
```
阅读全文