天天看點

求求你,别再用 System.out.println("");了!

1、日志架構

小張;開發一個大型系統: 

1、System.out.println("");将關鍵資料列印在控制台;去掉?寫在一個檔案?

2、架構來記錄系統的一些運作時資訊;日志架構 ;zhanglogging.jar;

3、高大上的幾個功能?異步模式?自動歸檔?xxxx?zhanglogging-good.jar?

4、将以前架構卸下來?換上新的架構,重新修改之前相關的API;

zhanglogging-prefect.jar;

5、JDBC---資料庫驅動;寫了一個統一的接口層;日志門面(日志的一個抽象層);logging-abstract.jar;給項目中導入具體的日志實作就行了;我們之前的日志架構都是實作的抽象層;

「市面上的日志架構:」

JUL、JCL、Jboss-logging、logback、log4j、log4j2、slf4j....

日志門面 (日志的抽象層) 日志實作
JCL(Jakarta Commons Logging) SLF4j(Simple Logging Facade for Java) 「jboss-logging」 Log4j JUL(java.util.logging) Log4j2 「Logback」

左邊選一個門面(抽象層)、右邊來選一個實作;

日志門面:SLF4J;

日子實作:Logback;

Spring boot:底層是Spring架構,Spring架構預設是用JCL;

Spring boot選用SLF4J和logback;

2. SLF4J 使用

1、如何在系統中使用SLF4j  https://www.slf4j.org」

以後開發的時候,日志記錄方法的調用,不應該來直接調用日志的實作類,而是調用日志抽象層裡面的方法;給系統裡面導入slf4j的jar和logback的實作jar。

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

「圖示:」

求求你,别再用 System.out.println("");了!

每一個日志的實作架構都有自己的配置檔案,使用slf4j以後,「配置檔案還是做成日志實作架構自己本身的配置檔案;」

2. 遺留問題

a(slf4j+logback): Spring(commons-logging)、Hibernate(jboss-logging)、MyBatis、xxxx

統一日志記錄,即使是别的架構和我一起統一使用slf4j進行輸出?

求求你,别再用 System.out.println("");了!

如何讓系統中所有的日志都統一到slf4j:

1.将系統中其他日志架構先排除出去;」

2.用中間包來替換原有的日志架構;」

3.我們導入slf4j其他的實作;」

3. 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>      

底層依賴關系:

求求你,别再用 System.out.println("");了!

總結:

1,SpringBoot底層也是使用slf4j+logback的方式進行日志記錄。

2,SpringBoot也把其他的日志都替換成了slf4j;

3,中間替換包?

@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;

<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的方式記錄日志,引入其他架構的時候,隻需要把這個架構依賴的日志架構排除掉即可;」

4. 日志的使用

1.預設配置

「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

Spring Boot修改日志的預設配置

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      
logging.file logging.path Example Description
(none) 隻在控制台輸出
指定檔案名 my.log 輸出日志到my.log檔案
指定目錄 /var/log 輸出到指定目錄的 spring.log 檔案中

2. 指定配置

給類路徑下放上每個日志架構自己的配置檔案即可;SpringBoot就不使用他預設配置的了。

Logging System Customization
Logback logback-spring.xml ,  logback-spring.groovy ,  logback.xml or logback.groovy
Log4j2 log4j2-spring.xml or  log4j2.xml
JDK (Java Util Logging) logging.properties

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]      

5. 切換日志架構

可以按照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>      

繼續閱讀