03.29 Spring Boot日誌框架實踐

Spring Boot日誌框架實踐

概述

Java應用中,日誌一般分為以下5個級別:

  • ERROR 錯誤信息

  • WARN 警告信息

  • INFO 一般信息

  • DEBUG 調試信息

  • TRACE 跟蹤信息

Spring Boot使用Apache的Commons Logging作為內部的日誌框架,其僅僅是一個日誌接口,在實際應用中需要為該接口來指定相應的日誌實現。

SpringBt默認的日誌實現是Java Util Logging,是JDK自帶的日誌包,此外SpringBt當然也支持Log4J、Logback這類很流行的日誌實現。

統一將上面這些 日誌實現 統稱為 日誌框架

下面我們來實踐一下!

使用Spring Boot Logging插件

  • 首先application.properties文件中加配置:

logging.level.root=INFO
  • 控制器部分代碼如下:

package com.hansonwang99.controller;import com.hansonwang99.K8sresctrlApplication;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/testlogging")public class LoggingTestController { private static Logger logger = LoggerFactory.getLogger(K8sresctrlApplication.class); @GetMapping("/hello")
public String hello() {
logger.info("test logging..."); return "hello";
}
}
  • 運行結果

Spring Boot日誌框架實踐

由於將日誌等級設置為INFO,因此包含INFO及以上級別的日誌信息都會打印出來

這裡可以看出,很多大部分的INFO日誌均來自於SpringBt框架本身,如果我們想屏蔽它們,可以將日誌級別統一先全部設置為ERROR,這樣框架自身的INFO信息不會被打印。然後再將應用中特定的包設置為DEBUG級別的日誌,這樣就可以只看到所關心的包中的DEBUG及以上級別的日誌了。

  • 控制特定包的日誌級別

application.yml中改配置

logging:
level:
root: error com.hansonwang99.controller: debug

很明顯,將root日誌級別設置為ERROR,然後再將 <code>com.hansonwang99.controller/<code> 包的日誌級別設為DEBUG,此即:即先禁止所有再允許個別的 設置方法

  • 控制器代碼

package com.hansonwang99.controller;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/testlogging")public class LoggingTestController { private Logger logger = LoggerFactory.getLogger(this.getClass()); @GetMapping("/hello")
public String hello() {
logger.info("test logging..."); return "hello";
}
}
  • 運行結果

Spring Boot日誌框架實踐

可見框架自身的INFO級別日誌全部藏匿,而指定包中的日誌按級別順利地打印出來

  • 將日誌輸出到某個文件中

logging:
level:

root: error com.hansonwang99.controller: debug
file: ${user.home}/logs/hello.log
  • 運行結果

Spring Boot日誌框架實踐

Spring Boot日誌框架實踐

使用Spring Boot Logging,我們發現雖然日誌已輸出到文件中,但控制檯中依然會打印一份,發現用 <code>org.slf4j.Logger/<code> 是無法解決這個問題的

Spring Boot日誌框架實踐

集成Log4J日誌框架

  • pom.xml中添加依賴

<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-web/<artifactid>
<exclusions>
<exclusion>

<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-logging/<artifactid>
/<exclusion>
/<exclusions>
/<dependency>
<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-log4j2/<artifactid>
/<dependency>
  • 在resources目錄下添加 <code>log4j2.xml/<code> 文件,內容如下:

<configuration>
<appenders>
<file>
<patternlayout>
/<file>
/<appenders>
<loggers>
<root>
<appender-ref>
/<root>
<logger>
/<loggers>/<configuration>
  • 其他代碼都保持不變

運行程序發現控制檯沒有日誌輸出,而hello2.log文件中有內容,這符合我們的預期:

Spring Boot日誌框架實踐

Spring Boot日誌框架實踐

Spring Boot日誌框架實踐

而且日誌格式和 <code>pattern="%d{HH:mm:ss,SSS} %p %c (%L) - %m%n"/<code> 格式中定義的相匹配

Log4J更進一步實踐

  • pom.xml配置:

<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-web/<artifactid>
<exclusions>
<exclusion>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-logging/<artifactid>
/<exclusion>
/<exclusions>
/<dependency>
<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-log4j2/<artifactid>
/<dependency>
  • log4j2.xml配置

<configuration>
<properties>
<property>springboot-web/<property>
<property>logs/${app_name}/<property>
/<properties>
<appenders>
<console>
<patternlayout>
/<console>
<rollingfile> filePattern="${log_path}/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz">
<filters>
<thresholdfilter>
<thresholdfilter> onMismatch="NEUTRAL" />
/<thresholdfilter>/<filters>
<patternlayout>

<policies>

<timebasedtriggeringpolicy>

<sizebasedtriggeringpolicy>
/<policies>

<defaultrolloverstrategy>
/<rollingfile>
<rollingfile> filePattern="${log_path}/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log.gz">
<filters>
<thresholdfilter>
<thresholdfilter> onMismatch="NEUTRAL" />
/<thresholdfilter>/<filters>
<patternlayout>
<policies>

<timebasedtriggeringpolicy>

<sizebasedtriggeringpolicy>
/<policies>

<defaultrolloverstrategy>
/<rollingfile>
<rollingfile> filePattern="${log_path}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log.gz">
<thresholdfilter>
<patternlayout>
<policies>

<timebasedtriggeringpolicy>

<sizebasedtriggeringpolicy>
/<policies>

<defaultrolloverstrategy>

/<rollingfile>
/<appenders>
<loggers>
<root>
<appender-ref>
<appender-ref>
<appender-ref>
<appender-ref>
/<root>
/<loggers>/<configuration>
  • 控制器代碼:

package com.hansonwang99.controller;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/testlogging")public class LoggingTestController { private final Logger logger = LogManager.getLogger(this.getClass()); @GetMapping("/hello")
public String hello() { for(int i=0;i<10_0000;i++){
logger.info("info execute index method");
logger.warn("warn execute index method");
logger.error("error execute index method");
} return "My First SpringBoot Application";
}
}
  • 運行結果

Spring Boot日誌框架實踐

Spring Boot日誌框架實踐

Spring Boot日誌框架實踐

日誌會根據不同的級別存儲在不同的文件,當日志文件大小超過2M以後會分多個文件壓縮存儲,生產環境的日誌文件大小建議調整為20-50MB。


分享到:


相關文章: