最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談Spring Boot日志框架實(shí)踐

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

Java應(yīng)用中,日志一般分為以下5個(gè)級(jí)別:

  1. ERROR 錯(cuò)誤信息
  2. WARN 警告信息
  3. INFO 一般信息
  4. DEBUG 調(diào)試信息
  5. TRACE 跟蹤信息

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

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

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

下面我們來實(shí)踐一下!

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

運(yùn)行結(jié)果

由于將日志等級(jí)設(shè)置為INFO,因此包含INFO及以上級(jí)別的日志信息都會(huì)打印出來

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

控制特定包的日志級(jí)別

application.yml中改配置

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

很明顯,將root日志級(jí)別設(shè)置為ERROR,然后再將 com.hansonwang99.controller 包的日志級(jí)別設(shè)為DEBUG,此即:即先禁止所有再允許個(gè)別的 設(shè)置方法

控制器代碼

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

運(yùn)行結(jié)果

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

將日志輸出到某個(gè)文件中

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

運(yùn)行結(jié)果

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

 

集成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>

其他代碼都保持不變

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

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

Log4J更進(jìn)一步實(shí)踐

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" />
    <!-- 限制單個(gè)文件大小 -->
    <SizeBasedTriggeringPolicy size="2 MB" />
   </Policies>
   <!-- 限制每天文件個(gè)數(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" />
    <!-- 限制單個(gè)文件大小 -->
    <SizeBasedTriggeringPolicy size="2 MB" />
   </Policies>
   <!-- 限制每天文件個(gè)數(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" />
    <!-- 限制單個(gè)文件大小 -->
    <SizeBasedTriggeringPolicy size="2 MB" />
   </Policies>
   <!-- 限制每天文件個(gè)數(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";
 }
}

運(yùn)行結(jié)果

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

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

相關(guān)文章

  • 入門java的第一步HelloWorld

    入門java的第一步HelloWorld

    這篇文章主要介紹了入門java的第一步-Hello,World,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的java初步學(xué)習(xí)具有一定的學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2021-04-04
  • spring*.xml配置文件明文加密的實(shí)現(xiàn)

    spring*.xml配置文件明文加密的實(shí)現(xiàn)

    這篇文章主要介紹了spring*.xml配置文件明文加密的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Spring gateway配置Spring Security實(shí)現(xiàn)統(tǒng)一權(quán)限驗(yàn)證與授權(quán)示例源碼

    Spring gateway配置Spring Security實(shí)現(xiàn)統(tǒng)一權(quán)限驗(yàn)證與授權(quán)示例源碼

    這篇文章主要介紹了Spring gateway配置Spring Security實(shí)現(xiàn)統(tǒng)一權(quán)限驗(yàn)證與授權(quán),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • Springboot之restTemplate的配置及使用方式

    Springboot之restTemplate的配置及使用方式

    這篇文章主要介紹了Springboot之restTemplate的配置及使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringBoot項(xiàng)目使用mybatis-plus逆向自動(dòng)生成全套代碼

    SpringBoot項(xiàng)目使用mybatis-plus逆向自動(dòng)生成全套代碼

    在JavaWeb工程中,每一個(gè)SSM新項(xiàng)目或者說是SpringBoot項(xiàng)目也好,都少不了model、controller、service、dao等層次的構(gòu)建。使用mybatis-plus逆向可以自動(dòng)生成,感興趣的可以了解一下
    2021-09-09
  • Nacos與SpringBoot實(shí)現(xiàn)配置管理的開發(fā)實(shí)踐

    Nacos與SpringBoot實(shí)現(xiàn)配置管理的開發(fā)實(shí)踐

    在微服務(wù)架構(gòu)中,配置管理是一個(gè)核心組件,而Nacos為此提供了一個(gè)強(qiáng)大的解決方案,本文主要介紹了Nacos與SpringBoot實(shí)現(xiàn)配置管理的開發(fā)實(shí)踐,具有一定的參考價(jià)值
    2023-08-08
  • Java繼承方法重寫實(shí)現(xiàn)原理及解析

    Java繼承方法重寫實(shí)現(xiàn)原理及解析

    這篇文章主要介紹了Java繼承方法重寫實(shí)現(xiàn)原理及解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Maven項(xiàng)目更換本地倉庫過程圖解

    Maven項(xiàng)目更換本地倉庫過程圖解

    這篇文章主要介紹了Maven項(xiàng)目更換本地倉庫過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • java跳板執(zhí)行ssh命令方式

    java跳板執(zhí)行ssh命令方式

    本文分享了在Java中使用跳板機(jī)執(zhí)行SSH命令的方法,并推薦了一些Maven依賴,希望這些信息對(duì)大家有所幫助
    2024-12-12
  • java使用zookeeper實(shí)現(xiàn)的分布式鎖示例

    java使用zookeeper實(shí)現(xiàn)的分布式鎖示例

    這篇文章主要介紹了java使用zookeeper實(shí)現(xiàn)的分布式鎖示例,需要的朋友可以參考下
    2014-05-05

最新評(píng)論

阿巴嘎旗| 雷波县| 获嘉县| 蓬莱市| 连城县| 永顺县| 临泉县| 黎平县| 微博| 海安县| 沈丘县| 玉田县| 怀安县| 海安县| 伊金霍洛旗| 仁寿县| 余庆县| 修武县| 耒阳市| 柘城县| 山东省| 陆河县| 通化县| 肃南| 临夏市| 镇沅| 梅州市| 饶阳县| 武平县| 岐山县| 宜兰县| 东兰县| 清苑县| 安阳县| 平凉市| 遂川县| 镇雄县| 如皋市| 永善县| 东乡| 太仆寺旗|