SpringBoot日誌原理解析

一、日誌框架

小張;開發一個大型系統;
一、System.out.println("");將關鍵數據打印在控制檯;去掉?寫在一個文件?
二、框架來記錄系統的一些運行時信息;日誌框架 ; zhanglogging.jar;
三、高大上的幾個功能?異步模式?自動歸檔?xxxx? zhanglogging-good.jar?
四、將之前框架卸下來?換上新的框架,從新修改以前相關的API;zhanglogging-prefect.jar;
五、JDBC---數據庫驅動;
寫了一個統一的接口層;日誌門面(日誌的一個抽象層);logging-abstract.jar;
給項目中導入具體的日誌實現就好了;咱們以前的日誌框架都是實現的抽象層;
市面上的日誌框架;
JUL、JCL、Jboss-logging、logback、log4j、log4j二、slf4j....html

 

 

左邊選一個門面(抽象層)、右邊來選一個實現;
日誌門面: SLF4J;
日誌實現:Logback;
SpringBoot:底層是Spring框架,Spring框架默認是用JCL;‘
SpringBoot選用 SLF4j和logback;web

二、SLF4j使用

一、如何在系統中使用SLF4j https://www.slf4j.org
之後開發的時候,日誌記錄方法的調用,不該該來直接調用日誌的實現類,而是調用日誌抽象層裏面的方法;
給系統裏面導入slf4j的jar和 logback的實現jarspring

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloWorld {
  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    logger.info("Hello World");
  }
}

圖示;數據庫

 

 

每個日誌的實現框架都有本身的配置文件。使用slf4j之後,配置文件仍是作成日誌實現框架本身自己的配置文
件;
二、遺留問題
a(slf4j+logback): Spring(commons-logging)、Hibernate(jboss-logging)、MyBatis、xxxx
統一日誌記錄,即便是別的框架和我一塊兒統一使用slf4j進行輸出?springboot

 

 

如何讓系統中全部的日誌都統一到slf4j;
一、將系統中其餘日誌框架先排除出去;
二、用中間包來替換原有的日誌框架;
三、咱們導入slf4j其餘的實現app

三、SpringBoot日誌關係

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring‐boot‐starter</artifactId>
</dependency>

SpringBoot使用它來作日誌功能;框架

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring‐boot‐starter‐logging</artifactId>
</dependency>

底層依賴關係異步

 

 

總結:
1)、SpringBoot底層也是使用slf4j+logback的方式進行日誌記錄
2)、SpringBoot也把其餘的日誌都替換成了slf4j;
3)、中間替換包?ui

@SuppressWarnings("rawtypes")
public abstract class LogFactory {
  static String UNSUPPORTED_OPERATION_IN_JCL_OVER_SLF4J =
  "http://www.slf4j.org/codes.html#unsupported_operation_in_jcl_over_slf4j";
  static LogFactory logFactory = new SLF4JLogFactory();

 

 

4)、若是咱們要引入其餘框架?必定要把這個框架的默認日誌依賴移除掉?
Spring框架用的是commons-logging;spa

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring‐core</artifactId>
  <exclusions>
    <exclusion>
      <groupId>commons‐logging</groupId>
      <artifactId>commons‐logging</artifactId>
    </exclusion>
  </exclusions>
</dependency>

SpringBoot能自動適配全部的日誌,並且底層使用slf4j+logback的方式記錄日誌,引入其餘框架的時候,只須要
把這個框架依賴的日誌框架排除掉便可;

四、日誌使用

一、默認配置
SpringBoot默認幫咱們配置好了日誌;

//記錄器
Logger logger = LoggerFactory.getLogger(getClass());
@Test
public void contextLoads() {
  //System.out.println();
  //日誌的級別;
  //由低到高 trace<debug<info<warn<error
  //能夠調整輸出的日誌級別;日誌就只會在這個級別以之後的高級別生效
  logger.trace("這是trace日誌...");
  logger.debug("這是debug日誌...");
  //SpringBoot默認給咱們使用的是info級別的,沒有指定級別的就用SpringBoot默認規定的級別;root級別
  logger.info("這是info日誌...");
  logger.warn("這是warn日誌...");
  logger.error("這是error日誌...");
}
日誌輸出格式:
%d表示日期時間,
%thread表示線程名,
%‐5level:級別從左顯示5個字符寬度
%logger{50} 表示logger名字最長50個字符,不然按照句點分割。
%msg:日誌消息,
%n是換行符
‐‐>
%d{yyyy‐MM‐dd HH:mm:ss.SSS} [%thread] %‐5level %logger{50} ‐ %msg%n

SpringBoot修改日誌的默認配置

logging.level.com.atguigu=trace
#logging.path=
# 不指定路徑在當前項目下生成springboot.log日誌
# 能夠指定完整的路徑;
#logging.file=G:/springboot.log
# 在當前磁盤的根路徑下建立spring文件夾和裏面的log文件夾;使用 spring.log 做爲默認文件
logging.path=/spring/log
# 在控制檯輸出的日誌的格式
logging.pattern.console=%d{yyyy‐MM‐dd} [%thread] %‐5level %logger{50} ‐ %msg%n
# 指定文件中日誌輸出的格式
logging.pattern.file=%d{yyyy‐MM‐dd} === [%thread] === %‐5level === %logger{50} ==== %msg%n

 

 

二、指定配置
給類路徑下放上每一個日誌框架本身的配置文件便可;SpringBoot就不使用他默認配置的了

 

 

logback.xml:直接就被日誌框架識別了;
logback-spring.xml:日誌框架就不直接加載日誌的配置項,由SpringBoot解析日誌配置,可使用SpringBoot
的高級Profile功能

<springProfile name="staging">
  <!‐‐ configuration to be enabled when the "staging" profile is active ‐‐>
  能夠指定某段配置只在某個環境下生效
</springProfile>

如:

<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<!‐‐
日誌輸出格式:
%d表示日期時間,
%thread表示線程名,
%‐5level:級別從左顯示5個字符寬度
%logger{50} 表示logger名字最長50個字符,不然按照句點分割。
%msg:日誌消息,
%n是換行符
‐‐>
<layout class="ch.qos.logback.classic.PatternLayout">
<springProfile name="dev">
<pattern>%d{yyyy‐MM‐dd HH:mm:ss.SSS} ‐‐‐‐> [%thread] ‐‐‐> %‐5level
%logger{50} ‐ %msg%n</pattern>
</springProfile>
<springProfile name="!dev">
<pattern>%d{yyyy‐MM‐dd HH:mm:ss.SSS} ==== [%thread] ==== %‐5level
%logger{50} ‐ %msg%n</pattern>
</springProfile>
</layout>
</appender>

若是使用logback.xml做爲日誌配置文件,還要使用profile功能,會有如下錯誤
no applicable action for [springProfile]

五、切換日誌框架

能夠按照slf4j的日誌適配圖,進行相關的切換;
slf4j+log4j的方式;

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐web</artifactId>
<exclusions>
<exclusion>
<artifactId>logback‐classic</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
<exclusion>
<artifactId>log4j‐over‐slf4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j‐log4j12</artifactId>
</dependency>

切換爲log4j2

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring‐boot‐starter‐logging</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐log4j2</artifactId>
</dependency>
相關文章
相關標籤/搜索