欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談Spring Boot日志框架實踐

 更新時間:2018年03月29日 10:40:16   作者:hansonwang  
這篇文章主要介紹了淺談Spring Boot日志框架實踐,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

  1. ERROR 錯誤信息
  2. WARN 警告信息
  3. INFO 一般信息
  4. DEBUG 調試信息
  5. TRACE 跟蹤信息

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

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

統(tǒng)一將上面這些 日志實現(xiàn) 統(tǒng)稱為 日志框架

下面我們來實踐一下!

使用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";
 }
}

運行結果

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

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

控制特定包的日志級別

application.yml中改配置

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

很明顯,將root日志級別設置為ERROR,然后再將 com.hansonwang99.controller 包的日志級別設為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";
 }
}

運行結果

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

將日志輸出到某個文件中

logging:
 level:
 root: error
 com.hansonwang99.controller: debug
 file: ${user.home}/logs/hello.log

運行結果

使用Spring Boot Logging,我們發(fā)現(xiàn)雖然日志已輸出到文件中,但控制臺中依然會打印一份,發(fā)現(xiàn)用 org.slf4j.Logger 是無法解決這個問題的

 

集成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目錄下添加 log4j2.xml 文件,內(nèi)容如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

 <appenders>
  <File name="file" fileName="${sys:user.home}/logs/hello2.log">
   <PatternLayout pattern="%d{HH:mm:ss,SSS} %p %c (%L) - %m%n"/>
  </File>
 </appenders>

 <loggers>

  <root level="ERROR">
   <appender-ref ref="file"/>
  </root>
  <logger name="com.hansonwang99.controller" level="DEBUG" />
 </loggers>

</configuration>

其他代碼都保持不變

運行程序發(fā)現(xiàn)控制臺沒有日志輸出,而hello2.log文件中有內(nèi)容,這符合我們的預期:

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

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配置

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="warn">
 <properties>

  <Property name="app_name">springboot-web</Property>
  <Property name="log_path">logs/${app_name}</Property>

 </properties>
 <appenders>
  <console name="Console" target="SYSTEM_OUT">
   <PatternLayout pattern="[%d][%t][%p][%l] %m%n" />
  </console>

  <RollingFile name="RollingFileInfo" fileName="${log_path}/info.log"
      filePattern="${log_path}/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz">
   <Filters>
    <ThresholdFilter level="INFO" />
    <ThresholdFilter level="WARN" onMatch="DENY"
         onMismatch="NEUTRAL" />
   </Filters>
   <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" />
   <Policies>
    <!-- 歸檔每天的文件 -->
    <TimeBasedTriggeringPolicy interval="1" modulate="true" />
    <!-- 限制單個文件大小 -->
    <SizeBasedTriggeringPolicy size="2 MB" />
   </Policies>
   <!-- 限制每天文件個數(shù) -->
   <DefaultRolloverStrategy compressionLevel="0" max="10"/>
  </RollingFile>

  <RollingFile name="RollingFileWarn" fileName="${log_path}/warn.log"
      filePattern="${log_path}/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log.gz">
   <Filters>
    <ThresholdFilter level="WARN" />
    <ThresholdFilter level="ERROR" onMatch="DENY"
         onMismatch="NEUTRAL" />
   </Filters>
   <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" />
   <Policies>
    <!-- 歸檔每天的文件 -->
    <TimeBasedTriggeringPolicy interval="1" modulate="true" />
    <!-- 限制單個文件大小 -->
    <SizeBasedTriggeringPolicy size="2 MB" />
   </Policies>
   <!-- 限制每天文件個數(shù) -->
   <DefaultRolloverStrategy compressionLevel="0" max="10"/>
  </RollingFile>

  <RollingFile name="RollingFileError" fileName="${log_path}/error.log"
      filePattern="${log_path}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log.gz">
   <ThresholdFilter level="ERROR" />
   <PatternLayout pattern="[%d][%t][%p][%c:%L] %m%n" />
   <Policies>
    <!-- 歸檔每天的文件 -->
    <TimeBasedTriggeringPolicy interval="1" modulate="true" />
    <!-- 限制單個文件大小 -->
    <SizeBasedTriggeringPolicy size="2 MB" />
   </Policies>
   <!-- 限制每天文件個數(shù) -->
   <DefaultRolloverStrategy compressionLevel="0" max="10"/>
  </RollingFile>

 </appenders>

 <loggers>


  <root level="info">
   <appender-ref ref="Console" />
   <appender-ref ref="RollingFileInfo" />
   <appender-ref ref="RollingFileWarn" />
   <appender-ref ref="RollingFileError" />
  </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";
 }
}

運行結果

日志會根據(jù)不同的級別存儲在不同的文件,當日志文件大小超過2M以后會分多個文件壓縮存儲,生產(chǎn)環(huán)境的日志文件大小建議調整為20-50MB。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 如何剔除eureka無效和down狀態(tài)的問題

    如何剔除eureka無效和down狀態(tài)的問題

    這篇文章主要介紹了如何剔除eureka無效和down狀態(tài)的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • springboot通過注解、接口創(chuàng)建定時任務詳解

    springboot通過注解、接口創(chuàng)建定時任務詳解

    使用SpringBoot創(chuàng)建定時任務其實是挺簡單的,這篇文章主要給大家介紹了關于springboot如何通過注解、接口創(chuàng)建這兩種方法實現(xiàn)定時任務的相關資料,需要的朋友可以參考下
    2021-07-07
  • Spring?Security權限管理實現(xiàn)接口動態(tài)權限控制

    Spring?Security權限管理實現(xiàn)接口動態(tài)權限控制

    這篇文章主要為大家介紹了Spring?Security權限管理實現(xiàn)接口動態(tài)權限控制,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • Spring+Hibernate+Struts(SSH)框架整合實戰(zhàn)

    Spring+Hibernate+Struts(SSH)框架整合實戰(zhàn)

    SSH是 struts+spring+hibernate的一個集成框架,是目前比較流行的一種Web應用程序開源框架。本篇文章主要介紹了Spring+Hibernate+Struts(SSH)框架整合實戰(zhàn),非常具有實用價值,需要的朋友可以參考下
    2018-04-04
  • Java編碼摘要算法實例解析

    Java編碼摘要算法實例解析

    這篇文章主要介紹了Java編碼摘要算法實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-01-01
  • 解析分別用遞歸與循環(huán)的方式求斐波那契數(shù)列的實現(xiàn)方法

    解析分別用遞歸與循環(huán)的方式求斐波那契數(shù)列的實現(xiàn)方法

    本篇文章是對分別用遞歸與循環(huán)的方式求斐波那契數(shù)列的方法進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • Java漏桶算法的簡單代碼實例

    Java漏桶算法的簡單代碼實例

    這篇文章主要介紹了Java漏桶算法的簡單代碼實例,漏桶算法的意義在于能夠平滑請求,不給下游服務造成過大壓力,特別適用于突發(fā)流量或者定時任務拉取大量數(shù)據(jù)時,需要處理大量數(shù)據(jù)或者請求的場景,需要的朋友可以參考下
    2024-01-01
  • Struts2實現(xiàn)上傳單個文件功能

    Struts2實現(xiàn)上傳單個文件功能

    這篇文章主要為大家詳細介紹了Struts2實現(xiàn)上傳單個文件功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • java list去重操作實現(xiàn)方式

    java list去重操作實現(xiàn)方式

    本文主要介紹了java list 去重的方法,其中有帶類型寫法和不帶類型寫法,并舉例測試,具有一定參考借鑒價值,希望能對有需要的小伙伴有所幫助
    2016-07-07
  • 解析java中的error該不該捕獲

    解析java中的error該不該捕獲

    這篇文章主要介紹了java中的error該不該捕獲,需要的朋友可以參考下
    2014-02-02

最新評論