Springboot使用@Slf4j注解简化日志代码

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);
    }
}

直接执行测试类,即可看见日志,如下图:
在这里插入图片描述

发布了21 篇原创文章 · 获赞 4 · 访问量 413

猜你喜欢

转载自blog.csdn.net/houpeibin2012/article/details/104399455
今日推荐