Annotation @ Slf4j,

Source: Internet
Author: User

Annotation @ Slf4j,

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

1. Add dependencies to the pom File

<Dependency>
<GroupId> org. projectlombok </groupId>
<ArtifactId> lombok </artifactId>
</Dependency>

Ii. Code

Package com. Taobao;
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 );
/**
* 1. Traditional log implementation
*/
@ Test
Public void test1 (){
Logger. debug ("debug message ");
Logger. warn ("warn message ");
Logger.info ("info message ");
Logger. error ("error message ");
Logger. trace ("trace message ");
}


/**
* 2. Implement logs by Annotation
*/
@ Test
Public void test2 (){
Log. Debug ("debug message ");
Log. Warn ("warn message ");
Log. Info ("info message ");
Log. Error ("error message ");
Log. Trace ("trace message ");
}

}

 

The output is as follows:

Because the default output is above info, we can see that debug and trace are not output

 

3. Note: If the variable log cannot be found after annotation @ Slf4j injection, install the lombok plug-in for the IDE ,,

The following uses idea as an example.

1. File → settings → Plugins, and then click "Browse repositories"

 

2. Enter lombok to search for the plug-in, click install to install it, and restart idea after installation.

 

 

At this time, enter the log and you will be prompted.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.