Springboot使用@Slf4j注解简化日志代码
1、pom文件需要加入对lombok的引用
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
注意:log4j的依赖在springboot下已经提供了,此处不用再重复引用。
2. idea开发工具需要安装lombok插件
我使用的是idea2019.2版本,按组合键ctrl+Alt+S 打开如下 窗口,按其中的步骤进行操作安装:
安装完毕后需要重启idea。
3. 测试代码
package com.test;
import junit.framework.TestCase;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class MyTests {
/**
* 使用断言
*/
@Test
public void test2() {
log.info("test hello 2");
TestCase.assertEquals(1, 1);
}
}
直接执行测试类,即可看见日志,如下图: