springboot implements Logback using log facade SLF4j and log

1. This paper mainly introduces how to use logback + slf4j to log in spring boot project.

logback consists of three main components: Loggers, Appenders and Layouts.

slf4j: Like jdbc, it defines a set of interfaces, which is a log facade, and enables fast switching between multiple log systems (by modifying configuration files)

logback: and log4j is the same author, is an upgraded version of log4j, the effect can be imagined.

logback is mainly divided into three modules, namely:

logback-core: Providing basic functionality is the basis for the other two modules
logback-classic: log4j upgrade, realizing self4j api
logback-access: Used to integrate with sevlet container and provide network access log

2. The Use of Logback in Primary Log Facade SLF4j and Logback

The first step is to use the spring boot framework to build the maven project:

 

 

 

 

 

 

The second step is to import dependencies into the Maven project of the spring boot framework:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

 

 

The third step is to create a new LoggerTest.java in test/java/com/imooc/

The first use:

private final Logger logger= LoggerFactory.getLogger(LoggerTest.class);
@RunWith(SpringRunner.class)
@SpringBootTest
public class LoggerTest {
    private final Logger logger= LoggerFactory.getLogger(LoggerTest.class);
    @Test
    public void test1(){
        logger.error("error...");
        logger.info("info...");
        logger.debug("debug...");
    }
}

Log Output Level: The bigger the number, the higher the level, the preferred output

 

The second use:

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class LoggerTest {
    @Test
    public void test1(){
        String name="yemeng";
        String password="123";
        log.info("name:{}, password:{}",name,password);
        log.error("error");
    }
}

 

Use configuration files

1.application.yml

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://192.168.1.110/sell?characterEncoding=utf-8&useSSL=false
  jpa:
    show-sql: true
#Project path
server:
  context-path: /sell
##Journal
logging:
  #format
  pattern:
    #date+information+Line feed
    console: "%d -%msg%n"
  #path: /var/log/tomcat/sell.log
  #Log Storage Path
  file: /var/log/tomcat/sell.log
  #Grade
  level: #debug
    com.imooc.LoggerTest:debug

2.logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>
                %msg%n
            </pattern>
        </layout>
    </appender>

<!-- output info log file-->
    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--Filtering strategy-->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <!--Discard matching log levels-->
            <level>ERROR</level>
            <onMatch>DENY</onMatch>
            <onMismatch>ACCEPT</onMismatch>
        </filter>
        <encoder>
            <pattern>
                %msg%n
            </pattern>
        </encoder>
    <!-- Rolling strategy-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    <!--  Route-->
            <fileNamePattern>var/log/tomcat/info.%d.log</fileNamePattern>
        </rollingPolicy>
    </appender>

    <!-- output error log file-->
    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--Filtering strategy-->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
        <encoder>
            <pattern>
                %msg%n
            </pattern>
        </encoder>
        <!--Rolling strategy-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- Route-->
            <fileNamePattern>var/log/tomcat/error.%d.log</fileNamePattern>
        </rollingPolicy>
    </appender>

    <root level="info">
        <appender-ref ref="consoleLog"/>
        <appender-ref ref="fileInfoLog"/>
        <appender-ref ref="fileErrorLog"/>
    </root>
</configuration>

Keywords: Java Spring Tomcat JDBC log4j

Added by DarkArchon on Mon, 07 Oct 2019 05:55:51 +0300