SpringBoot中配置log4j2日志詳解
1. 概述
官方參考文檔:Log4j
Apache Log4j2 是對原先的 Log4j 項目的升級版本,參考了 logback 的一些優(yōu)秀的設(shè)計,并且修復(fù)了一些問題,因此帶來了一些重大的提升。
- 異常處理,在 logback 中,Appender 中的異常不會被應(yīng)用感知到,但是在 log4j2 中,提供了一些異常處理機(jī)制;
- 性能提升,log4j2 相較于 log4j 和 logback 都具有明顯的性能提升,有18倍性能提升;
- 自動裝載配置,參考了 logback 的設(shè)計,當(dāng)然會提供自動刷新參數(shù)配置,最實(shí)用的就是我們在生產(chǎn)上可以動態(tài)的修改日志級別而不需要重啟應(yīng)用;
- 無垃圾機(jī)制,log4j2 在大部分情況下,都可以使用其設(shè)計的一套無垃圾機(jī)制(對象重用、內(nèi)存緩沖),避免頻繁的日志收集導(dǎo)致的 JVM gc。
2. 案例與解析
2.1 引入依賴
SpringBoot 的 starter 自帶的是 logback 日志,若要使用 log4j2 日志,需要引入對應(yīng)依賴。
logback 日志和 log4j2 日志都是對 slf4j 門面的實(shí)現(xiàn),只能存在一個,且必須存在一個,不存在或者存在多個都會出錯。如果兩者都存在就會出現(xiàn)以下的問題:
因此,在使用 log4j2 日志時,必須要在依賴中把 logback 給 exclude 掉。 并且,使用 log4j2 日志還需要適配器 log4j-slf4j-impl,它跟 SpringBoot 的 starter 自帶的 log4j-to-slf4j 是相互沖突的,因此還需要將 log4j-to-slf4j 也 exclude 掉。
因?yàn)?SpringBoot 的 starter 中已經(jīng)帶有 slf4j 門面了,因此無需再引入 slf4j 依賴。
<!-- 使用 log4j2 的適配器進(jìn)行綁定 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.19.0</version> </dependency> <!-- log4j2 日志門面 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.19.0</version> </dependency> <!-- log4j2 日志實(shí)面 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.19.0</version> </dependency>
2.2 定義日志文件 log4j2.xml
<?xml version="1.0" encoding="UTF-8" ?> <configuration status="warn" monitorInterval="5"> <properties> <property name="LOG_HOME" value="logs"/> </properties> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] [%-5level] %c{36}:%L --- %m%n"/> </Console> <File name="File" fileName="${LOG_HOME}/file-log4j2.log"> <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%-5level] %l %c{36} - %m%n"/> </File> <RandomAccessFile name="AccessFile" fileName="${LOG_HOME}/myAcclog.log" immediateFlush="true"> <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%-5level] %l %c{36} - %m%n"/> </RandomAccessFile> <RollingFile name="RollingFile" fileName="${LOG_HOME}/log4j2.log" filePattern="logs/log4j2.%d{yyyy-MM-dd-HH-mm}.%i.log"> <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss}] [%-5level] %l %c{36} - %msg%n"/> <Policies> <OnStartupTriggeringPolicy/> <SizeBasedTriggeringPolicy size="10 KB"/> <TimeBasedTriggeringPolicy/> </Policies> <DefaultRolloverStrategy max="30"/> </RollingFile> </Appenders> <Loggers> <Root level="INFO"> <AppenderRef ref="Console"/> <AppenderRef ref="File"/> <AppenderRef ref="AccessFile"/> <AppenderRef ref="RollingFile"/> </Root> </Loggers> </configuration>
2.3 在 SpringBoot 項目的配置文件中配置
在 SpringBoot 的 application.properties (或者ymal格式的application.yaml) 中指明日志配置文件
# log4j2 logging.config=classpath:log4j2.xml
2.4 日志配置解析
Appenders
在 Appenders
中定義了 4 個 Appender,分別對應(yīng)了 Console
、File
、 RandomAccessFile
和 RollingFile
4種類型。
- Console:控制臺輸出日志;
- File: 日志全部輸出到一個文件中;
- RandomAccessFile:參考 RandomAccessFile,RandomAccessFile 總是將日志寫入到緩存中,然后再寫入到磁盤,并且寫入緩存的過程是不能被關(guān)掉的,而 FileAppender 中寫入緩存的過程是可以被關(guān)閉的。若將 immediateFlush 屬性設(shè)置為 true,那么每寫完一條日志到緩存后都會寫入到磁盤中;
- RollingFile:滾動日志,根據(jù)日期和日志文件大小滾動;
Loggers
- Root:是根 logger,所有根據(jù)類的全路徑名定位不到的 logger 都使用 Root 中定義的 Appender 來打印日志;
- Logger:定義一個具有具體名字的 logger,如 com.foo.Bar,那么在要使用這個 logger 的地方 LogManager.getLogger("com.foo.Bar") 即可得到這個 logger,就可以用這個 logger 下定義的 Appender 來打印日志了;
- 如果定位不到 logger,則使用 Root 作為默認(rèn)的 logger。因此,我們可以在 Root 中定義默認(rèn)的日志配置,如果需要定義某個特定的 logger,采用不同的日志級別,那么就可以單獨(dú)定義一個logger,在需要的地方用這個 logger 就可以了,十分方便;
Log Format 格式及其說明
- -d [%-6p] %c{1} - %m%n
Using [%-6p], the logging level should be left-justified to a width of six characters. Use it with a pretty printed log level. It will generate the below output:
2016-06-20 19:21:05,271 [DEBUG ] Log4j2HelloWorldExample - Debug Message Logged !! 2016-06-20 19:21:05,272 [INFO ] Log4j2HelloWorldExample - Info Message Logged !!
- -d [%-6p] %c{3} - %m%n
Use %{1} for printing the complete package level. It will generate the below output:
2016-06-20 19:22:05,379 [DEBUG ] com.howtodoinjava.log4j2.examples.Log4j2HelloWorldExample - Debug Message Logged !! 2016-06-20 19:22:05,380 [INFO ] com.howtodoinjava.log4j2.examples.Log4j2HelloWorldExample - Info Message Logged !!
- %{3}
will print the package level upto two levels.
2016-06-20 19:23:48,202 [DEBUG ] log4j2.examples.Log4j2HelloWorldExample - Debug Message Logged !! 2016-06-20 19:23:48,204 [INFO ] log4j2.examples.Log4j2HelloWorldExample - Info Message Logged !!
- %d{yyyy/MM/dd HH:mm:ss,SSS} [%-6p] %c{1} - %m%n
Use it for custom date format. It will generate the below output:
2016/06/20 19:24:45,076 [DEBUG ] Log4j2HelloWorldExample - Debug Message Logged !! 2016/06/20 19:24:45,078 [INFO ] Log4j2HelloWorldExample - Info Message Logged !!
3. 測試
3.1 控制臺輸出的日志
3.2 日志文件中輸出的日志
到此這篇關(guān)于SpringBoot中配置log4j2日志詳解的文章就介紹到這了,更多相關(guān)SpringBoot配置log4j2日志內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
掌握SpringMVC中@InitBinder的實(shí)際應(yīng)用
這篇文章主要介紹了掌握SpringMVC中@InitBinder的實(shí)際應(yīng)用,@InitBinder是Spring MVC框架中的一個注解,用于自定義數(shù)據(jù)綁定的方法,通過在控制器中使用@InitBinder注解,可以將特定的數(shù)據(jù)綁定邏輯應(yīng)用于請求參數(shù)的處理過程中,需要的朋友可以參考下2023-10-10Springmvc自定義類型轉(zhuǎn)換器實(shí)現(xiàn)步驟
這篇文章主要介紹了Springmvc自定義類型轉(zhuǎn)換器實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08IntelliJ IDEA中ajax開發(fā)實(shí)現(xiàn)分頁查詢示例
這篇文章主要介紹了IntelliJ IDEA中ajax開發(fā)實(shí)現(xiàn)分頁查詢,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03SpringBoot工程打包與運(yùn)行的實(shí)現(xiàn)詳解
本文主要介紹了SpringBoot工程的打包與運(yùn)行的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Spring中的兩種代理JDK和CGLIB的區(qū)別淺談
本篇文章中主要介紹了Spring中的兩種代理JDK和CGLIB的區(qū)別淺談,詳解的介紹了JDK和CGLIB的原理和方法,有需要的朋友可以了解一下2017-04-04Java訂單30分鐘未支付自動取消該怎么實(shí)現(xiàn)
在開發(fā)中往往會遇到一些關(guān)于延時任務(wù)的需求,例如生成訂單30分鐘未支付,則自動取消,下面這篇文章主要給大家介紹了關(guān)于Java訂單30分鐘未支付自動取消該怎么實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2023-03-03Java實(shí)現(xiàn)的模糊匹配某文件夾下的文件并刪除功能示例
這篇文章主要介紹了Java實(shí)現(xiàn)的模糊匹配某文件夾下的文件并刪除功能,涉及java針對目錄與文件的遍歷、匹配、判斷、刪除等相關(guān)操作技巧,需要的朋友可以參考下2018-02-02