1. 程式人生 > >springBoot中Logback日誌的使用

springBoot中Logback日誌的使用

1、lombok工具的使用

1、未使用lombok之前的測試類如下:

package com.imooc.sell;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

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

2、在pom.xml檔案中加入lomback依賴後,程式碼如下:

package com.imooc.sell;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
//增加該註解
@Slf4j
public class LoggerTest {


    @Test
    public void test1() {
        //直接使用log物件列印日誌
        log.info
("info log...."); log.debug("debug log...."); log.error("error log...."); } }

2、日誌的配置

1、application.yml配置(簡單,功能單一):

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://192.168.19.69/sell?characterEncoding=utf-8&useSSL=false
  jpa:
    show-sql: true
server:
  context-path: /wyh_sell
##############日誌相關資訊配置##############
logging:
 # 配置日誌的格式
  pattern:
    console: "%d - %msg%n"
  # 配置日誌的輸出路徑(預設是spring.log)
  path:  D:/log
  # 指定將日誌輸出到某個檔案中(file和path取其中一個即可)
  file:  D:/log/shell.log
  #級別
  level: debug

2、logback-spring.xml配置(複雜,功能強大):

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <!--第一個配置項:這裡是日誌輸出格式-->
    <appender name="consoleLogger" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>
                %d - %msg%n
            </pattern>
        </layout>
    </appender>

    <!--第二個配置項:將info日誌輸出到哪個info檔案中-->
    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--只儲存info型別的日誌-->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>DENY</onMatch>
            <onMismatch>ACCEPT</onMismatch>
        </filter>
        <encoder>
            <pattern>
                %msg%n
            </pattern>
        </encoder>
        <!--滾動策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--路徑-->
            <fileNamePattern>D:/log/info.%d.log</fileNamePattern>
        </rollingPolicy>
    </appender>

    <!--第三個配置項:將日誌輸出到哪個錯誤日誌檔案中-->
    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--只儲存error型別的日誌-->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
        <encoder>
            <pattern>
                %msg%n
            </pattern>
        </encoder>
        <!--滾動策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--路徑-->
            <fileNamePattern>D:/log/error.%d.log</fileNamePattern>
        </rollingPolicy>
    </appender>

    <!--引用哪個配置-->
    <root level="info">
        <appender-ref ref="consoleLogger"/>
        <appender-ref ref="fileInfoLog"/>
        <appender-ref ref="fileErrorLog"/>
    </root>
</configuration>