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.