@ slf4j

If you do not want to write every private final Logger logger = LoggerFactory.getLogger (XXX.class); you can use an annotation @ Slf4j

First, in the pom file to the dependence

org.projectlombok lombok

Second, the code

package com.sell;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**

  • Log test
    * /

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class LoggerTest {

private  final Logger logger = LoggerFactory.getLogger(LoggerTest.class);
/**
 * 一、传统方式实现日志
 */
@Test
public  void test1(){
    logger.debug("debug message");
    logger.warn("warn message");
    logger.info("info message");
    logger.error("error message");
    logger.trace("trace message");
}


/**
 * 二、注解方式实现日志
 */
@Test

public void test2(){
log.debug(“debug message”);
log.warn(“warn message”);
log.info(“info message”);
log.error(“error message”);
log.trace(“trace message”);
}

}

Output is as follows:

Here Insert Picture Description

Because the default output is above the info, debug seen in the figure below, the trace is not output

Here Insert Picture Description

Third, pay attention: If you can not find the notes @ Slf4j injection variable log, then give IDE plug-ins installed lombok ,,

Below idea, for example

1, File → settings → Plugins, then click on the "Browse repositories" in Fig.

Here Insert Picture Description

2, input lombok search plug-in, point install installation, reboot after installing idea

Here Insert Picture Description

This time you can enter log prompted the

Published 18 original articles · won praise 5 · Views 6721

Guess you like

Origin blog.csdn.net/qq_28687183/article/details/103061348