Springboot log configuration slf4j_SpringBoot project configuration Log log service

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

4164c8cdc4490e666d0865ac64349603.png

The first step is to modify the pom file first and let him download the dependencies

57bec334b17ae59e07ec8db3cd13db67.png

 <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

4b7d1fba2de80756cf7128ad528f5007.png

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

c5ccc21b46e2d4e3dd5e692f77c4b9cd.png

logging:  config: classpath:logback.xml

The fourth step is to add the logback.xml file for log related configuration

1b4edf1438199db37a3598098f30a9c0.png

<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

97adb91137863f4f0b2d821ef0159471.png

646edd1b59b54ae20858bd73d96f4818.png

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.

Guess you like

Origin blog.csdn.net/xulong5000/article/details/115249576