1. 程式人生 > 實用技巧 >spring-boot-learning 日誌相關

spring-boot-learning 日誌相關

sprint-boot 日誌

市面上的日誌框架;

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

SpringBoot:底層是Spring框架,Spring框架預設是用JCL;‘

==SpringBoot選用 SLF4j和logback;==

日誌記錄方法的呼叫,不應該來直接呼叫日誌的實現類,而是呼叫日誌抽象層裡面的方法;

給系統裡面匯入slf4j的jar和 logback的實現jar

使用手冊:http://www.slf4j.org/manual.html

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抽象日誌框架進行輸出

springBoot日誌關係
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

SpringBoot使用spring-boot-starter-logging來做日誌功能

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

1)、SpringBoot底層也是使用slf4j+logback的方式進行日誌記錄

​2)、SpringBoot也把其他的日誌都替換成了slf4j;
中間替換包如下:

==SpringBoot能自動適配所有的日誌,而且底層使用slf4j+logback的方式記錄日誌,

引入其他框架的時候,只需要把這個框架依賴的日誌框架排除掉即可;

排除的格式;

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

使用日誌

  • ERROR 錯誤資訊
  • WARN 警告資訊
  • INFO 一般資訊
  • DEBUG 除錯資訊
  • TRACE 跟蹤資訊

預設配置

@SpringBootTest
class SpLoggingApplicationTests {
    //記錄器
    Logger logger = LoggerFactory.getLogger(getClass());
    @Test
    void contextLoads() {
        //日誌的級別,由低到高trace debug  info  warn  error
        //調整輸出日誌級別,日誌只會在這個級別或者以後的高級別列印
        // //上線只能只想輸出warn,和error級別的日誌
        logger.trace("只是日誌");
        logger.debug("這是debug日誌");
        //springboot 預設給我使用的使info級別,
        //沒有指定級別的就用springboot 預設規定的級別,root級別
        logger.info("這是info資訊");
        logger.warn("這是warn日誌");
        logger.error("error rizhi ");


    }

}

結果 :

因為設定的預設的logging.level是info所以debug和trance都沒有顯示出來
在配置檔案裡面設定就可以了,logging.level.com = debug ,必須在level後面還要加上路徑

修改結果:

可以在application.properties裡面修改日誌配置

logging.level.com.quan.splogging=trace#指定那個包或者哪個類的輸出日誌級別
#不指定路徑就在當前專案下生成springbot.log日誌
#可以指定完整的路徑名稱
#logging.file.name=quan.log
#一般指定目錄
#不指定磁碟,預設在所在磁碟的根路徑下建立spring資料夾裡面的log資料夾
#使用spring.log為檔名字
logging.file.path=E:/spring/log
#在控制檯輸出的日誌格式
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
#指定檔案中的日誌輸出格式
logging.pattern.file==

列子:

logging:
  level:
    com: trace
  pattern:
    console: 時間:%d{yyyy~MM~dd=HH:mm:ss} 執行緒名字:[[%thread]] 詳情:%msg%n

熱:

時間:2020~08~06=15:02:22 執行緒名字:[[main]] 詳情:Running with Spring Boot v2.3.2.RELEASE, Spring v5.2.8.RELEASE
時間:2020~08~06=15:02:22 執行緒名字:[[main]] 詳情:No active profile set, falling back to default profiles: default
時間:2020~08~06=15:02:23 執行緒名字:[[main]] 詳情:Initializing ExecutorService 'applicationTaskExecutor'
時間:2020~08~06=15:02:23 執行緒名字:[[main]] 詳情:Started HuolalaApplicationTests in 16.355 seconds (JVM running for 17.283)

時間:2020~08~06=15:02:23 執行緒名字:[[main]] 詳情:djdijdi
時間:2020~08~06=15:02:23 執行緒名字:[[main]] 詳情:debugdebug
時間:2020~08~06=15:02:23 執行緒名字:[[main]] 詳情:errorerror

logging.file.name和logging.file.path的區別:

日誌格式的內容修改含義:

    日誌輸出格式:
        %d表示日期時間,
        %thread表示執行緒名,
        %-5level:級別從左顯示5個字元寬度
        %logger{50} 表示logger名字最長50個字元,否則按照句點分割。 
        %msg:日誌訊息,
        %n是換行符

指定配置:

給類路徑下放上每個日誌框架自己的配置檔案即可:SpringBoot就不適用預設配置了,

logback.xml:直接就被日誌框架識別了;

logback-spring.xml日誌框架就不直接載入日誌的配置項,由SpringBoot解析日誌配置,可以使用SpringBoot的高階Profile功能

沒使用Profile的:

<configuration>
<!--      設定日誌輸出的格式,以便後面使用root引用   -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>logbackyml %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

<!-- logger的值可以不區分大小寫   用於設定那個包的或者具體類的日誌列印級別       -->
    <logger name="com.quan.learning" level="INFO"/>

    <!-- Strictly speaking, the level attribute is not necessary since -->
    <!-- the level of the root level is set to DEBUG by default.       -->
<!--    root節點必須在appender下面,主要的作用就是,對appdender進行管理
需要哪個就加入哪個,
而且這裡的日誌級別是總級別,最低能夠打出的日誌級別,如果appender比他高就取高的
-->
    <root level="INFO">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

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

如果使用logback.xml作為日誌配置檔案,還要使用springProfile那就會報錯

切換日誌框架:
可以按照slf4j的日誌適配圖,進行相關的切換;

排除所需要排除的,

新增所需要的東西

例子:SLF4j+log4j2

直接

直接exclusion spring-boot-starter-logging,然後加上要實現的

     <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>
<!--引入新的log實現框架達到SLF4j+log4j,不過一般都不需要,因為有bug-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
            <version>2.3.2.RELEASE</version>
        </dependency>

配置:

logging:
  level:
    com: trace
  pattern:
    console: log4j2的日誌===時間:%d{yyyy~MM~dd=HH:mm:ss} 執行緒名字:[[%thread]] 詳情:%msg%n

結果:

log4j2的日誌===時間:2020~08~06=16:22:47 執行緒名字:[[main]] 詳情:Initializing ExecutorService 'applicationTaskExecutor'
log4j2的日誌===時間:2020~08~06=16:22:47 執行緒名字:[[main]] 詳情:Started HuolalaApplicationTests in 21.406 seconds (JVM running for 27.56)

log4j2的日誌===時間:2020~08~06=16:22:47 執行緒名字:[[main]] 詳情:djdijdi
log4j2的日誌===時間:2020~08~06=16:22:47 執行緒名字:[[main]] 詳情:debugdebug
log4j2的日誌===時間:2020~08~06=16:22:47 執行緒名字:[[main]] 詳情:errorerror

log4j2的日誌===時間:2020~08~06=16:22:47 執行緒名字:[[SpringContextShutdownHook]] 詳情:Shutting down ExecutorService 'applicationTaskExecutor'