springboot依赖log4j
时间: 2023-10-17 12:37:35 浏览: 168
springboot整合log4j入门程序
在Spring Boot中使用Log4j的方法如下:
1. 在pom.xml文件中引入log4j的依赖:
```
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-log4j -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
```
2. 关闭Spring Boot自带的Logback依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- exclude Logback -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
```
3. 创建一个测试类,并在该类中使用Logger记录日志:
```java
package com.boot.zhiyi.logTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class logTest {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Test
public void testLog() {
logger.error("用户为绑定就开始缴费");
}
}
```
通过以上步骤,你就可以在Spring Boot项目中使用Log4j来记录日志了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [springBoot使用log4j详解](https://blog.csdn.net/zhiyikeji/article/details/85955010)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文