Configure slf4j steps
1. The pom file needs to introduce dependencies
2. Create a TestLog class
3. Write the path of log.xml in yml or properties
4. Add a Logback.xnk file. First come a picture of the completed configuration
The first step is to modify the pom file first and let him download the dependencies
<dependency> <groupId>org.slf4jgroupId> <artifactId>slf4j-apiartifactId> <version>1.7.25version> dependency> <dependency> <groupId>ch.qos.logbackgroupId> <artifactId>logback-coreartifactId> <version>1.2.3version> dependency> <dependency> <groupId>ch.qos.logbackgroupId> <artifactId>logback-classicartifactId> <version>1.2.3version> dependency>
The second step is to write a main() function as the startup entry, and create a resource folder to store static files
import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class TestLog { static Logger logger = LoggerFactory.getLogger(TestLog.class); public static void main(String[] arge) { logger.debug(" debug"); logger.info(" info"); logger.error(" error"); logger.warn(" warn"); }}
The third step is to add an application.yml or application.properrties configuration file. Add the following configuration to specify the configuration xml path of logback
logging: config: classpath:logback.xml
The fourth step is to add the logback.xml file for log related configuration
<appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger -%msg%npattern> layout>appender><appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger -%msg%npattern> encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${logPath}/info/%d.logfileNamePattern> <maxHistory>${maxHistory}maxHistory> rollingPolicy> appender><appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERRORlevel> filter> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger -%msg%npattern> encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${logPath}/error/%d.logfileNamePattern> <maxHistory>60maxHistory> rollingPolicy>appender><root level="info"> <appender-ref ref="consoleLog" /> <appender-ref ref="fileInfoLog" /> <appender-ref ref="fileErrorLog" />root>
After running, you can see the log output to the console and file
It is found that there is no debug log output, because the record specified by our node is at the info level, he will only record the info level or higher, and the rest will be ignored to ensure that the generation environment produces too many logs.