市場上的日誌框架
1)日誌門面最常用的是slf4j
2)日誌實現最常用的是logback
和log4j2
, 當然log4j也還有人使用,只是log4j比較老舊了
3)log4j
、logback
、slf4j
都是出自同一個人之手,他首先寫了log4j,後來發現log4j有性能問題,就又寫了logback . . .
4)日誌門面slf4j
不是一個真正的日誌實現,它是抽象層的api框架,它允許你在後臺使用任意一個日誌實現框架,實現系統與日誌實現框架的解耦
spring boot日誌框架關係
1)spring boot底層使用slf4j + logback框架來實現日誌記錄。
2)slf4j是日誌抽象層api框架,logback是日誌實現層框架。也就會說我們是面向slf4j來編程的,具體靠logback來實現。
面向slf4j編程
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");
}
}
具體靠logback來實現
1)logback的具體實現就不需要我們來操心了,當然我們可以修改一些默認配置。
#日誌記錄
logging:
#日誌級別
level:
#可以調整不同目錄的日誌級別(注意:必須指定包,否則報錯)
com.cd.example.one: trace
com.cd.example.two: debug
com.cd.example.three: info
#日誌文件
file:
#日誌文件完整路徑(含文件名)
name: D:\idea\logs\example-loggin.log
#日誌文件根路徑(spring boot默認使用spring.log作爲日誌文件)
#path: D:\idea\log
#文件大小(默認10MB)
max-size: 10MB
#文件保存週期(默認7天,七天之後就會刪除)
max-history: 7
#日誌輸出格式
pattern:
#在控制檯輸出的日誌格式
console: -%d{yyyy-MM-dd HH:mm:ss} -- %-5level -- [%thread] -- %logger{50} ---> %msg %n
#在文件輸出的日誌格式
file: '%d{yyyy-MM-dd hh:mm:ss} -- %-5level -- [%thread] -- %logger{50} --- %msg %n'
移除默認日誌框架
1)spring boot默認日誌框架都依賴於spring-boot-starter-logging
(具體在pom.xml文件中按crl + shift + alt + u
查看依賴樹)。
2)移除默認日誌框架的方法:
1)打開依賴樹,選中spring-boot-starter-logging
,快捷鍵shift + delete
移除。
2)在spring-boot-starter-web
依賴中使用<exclusion>
移除默認日誌框架
<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>
切換爲log4j2日誌框架
1)添加spring-boot-starter-log4j2
依賴
<!--log4j2日誌框架-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
2)spring boot官方文檔的提示,spring boot集成log4j2框架,那麼日誌配置文件只能是log4j2.xml
或log4j2-spring.xml
。
3)spring boot官方建議使用log4j2-spring.xml
作爲log4j2框架的日誌配置文件,因爲帶-spring
後綴的配置文件可以使用spring boot提供的一些高級功能,如profile
多環境日誌輸出功能
log4j2-spring.xml
<?xml version="1.0" encoding="UTF-8" ?>
<configuration >
<Appenders>
<!--當前爲dev環境時,該配置生效-->
<springProfile name="dev">
<!--控制檯輸出日誌信息-->
<Console name="Console" target="SYSTEM_OUT">
<!--日誌格式-->
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} -- %-5level -- [%thread] -- %logger{50} ---> %msg %n"/>
</Console>
</springProfile>
<!--日誌文件記錄日誌信息-->
<RollingFile name="log_file"
fileName="D:\idea\logs\example.log"
filePattern="D:\idea\logs\example-%d{yyyy-MM-dd}-%i.log">
<!--文件滾動策略-->
<Policies>
<!--基於時間的滾動策略,默認是1小時,我這裏設置24小時滾動一次-->
<TimeBasedTriggeringPolicy interval="24"/>
<!--基於大小的滾動策略-->
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<!--日誌格式-->
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} -- %-5level -- [%thread] -- %logger{50} ---> %msg %n"/>
</RollingFile>
</Appenders>
<Loggers>
<!--只有在Root中引入appender,appender纔會生效-->
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="log_file"/>
</Root>
</Loggers>
</configuration>
1) 部分開發者在記錄日誌文件時,喜歡將不同級別的日誌信息分開獨自記錄:比如debug日誌信息單獨記錄到一個日誌文件中,info日誌信息記錄到一個日誌文件中,warn日誌信息記錄到一個日誌文件中,error日誌信息記錄到一個日誌文件中,那麼就可以使用臨界值過濾器 <ThresholdFilter/>
來實現
2) 下面配置的意思是:一個臨界值過濾器
接受debug
級別以上的日誌、另一個臨界值過濾器
拒絕info
級別以上的日誌。相當於只記錄debug級別的信息
<!--接受debug級別以上的日誌-->
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
<!--拒絕info級別以上的日誌-->
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
log4j2-spring.xml
<?xml version="1.0" encoding="UTF-8" ?>
<configuration >
<!--參數配置-->
<Properties>
<!--日誌文件根路徑-->
<property name="log_path" value="D:\idea\logs"/>
</Properties>
<!--日誌輸出目標-->
<Appenders>
<!--當前爲dev環境時,該配置生效-->
<springProfile name="dev">
<!--控制檯輸出日誌信息-->
<Console name="Console" target="SYSTEM_OUT">
<!--日誌格式-->
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} -- %-5level -- [%thread] -- %logger{50} ---> %msg %n"/>
</Console>
</springProfile>
<!--日誌文件輸出debug級別日誌信息-->
<RollingFile name="debug_file"
fileName="${log_path}\debug\example_debug.log"
filePattern="${log_path}\debug\example_debug-%d{yyyy-MM-dd}-%i.log">
<!--文件滾動策略-->
<Policies>
<!--基於時間的滾動策略,默認是1小時,我這裏設置24小時滾動一次-->
<TimeBasedTriggeringPolicy interval="24"/>
<!--基於大小的滾動策略-->
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<!--日誌格式-->
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} -- %-5level -- [%thread] -- %logger{50} ---> %msg %n"/>
<!--臨界值過濾器-->
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
</RollingFile>
<!--日誌文件輸出日誌信息-->
<RollingFile name="info_file"
fileName="${log_path}\info\example_info.log"
filePattern="${log_path}\info\example_info-%d{yyyy-MM-dd}-%i.log">
<!--文件滾動策略-->
<Policies>
<!--基於時間的滾動策略,默認是1小時,我這裏設置24小時滾動一次-->
<TimeBasedTriggeringPolicy interval="24"/>
<!--基於大小的滾動策略-->
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<!--日誌格式-->
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} -- %-5level -- [%thread] -- %logger{50} ---> %msg %n"/>
<!--臨界值過濾器-->
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="warn" onMatch="DNEY" onMismatch="NEUTRAL"/>
</RollingFile>
<!--日誌文件輸出日誌信息-->
<RollingFile name="warn_file"
fileName="${log_path}\warn\example_warn.log"
filePattern="${log_path}\warn\example_warn-%d{yyyy-MM-dd}-%i.log">
<!--文件滾動策略-->
<Policies>
<!--基於時間的滾動策略,默認是1小時,我這裏設置24小時滾動一次-->
<TimeBasedTriggeringPolicy interval="24"/>
<!--基於大小的滾動策略-->
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<!--日誌格式-->
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} -- %-5level -- [%thread] -- %logger{50} ---> %msg %n"/>
<!--臨界值過濾器-->
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
</RollingFile>
<!--日誌文件輸出日誌信息-->
<RollingFile name="error_file"
fileName="${log_path}\error\example_error.log"
filePattern="${log_path}\error\example_error-%d{yyyy-MM-dd}-%i.log">
<!--文件滾動策略-->
<Policies>
<!--基於時間的滾動策略,默認是1小時,我這裏設置24小時滾動一次-->
<TimeBasedTriggeringPolicy interval="24"/>
<!--基於大小的滾動策略-->
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<!--日誌格式-->
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} -- %-5level -- [%thread] -- %logger{50} ---> %msg %n"/>
<!--臨界值過濾器-->
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL"/>
</RollingFile>
</Appenders>
<Loggers>
<!--只有在Root中引入appender,appender纔會生效-->
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="debug_file"/>
<AppenderRef ref="info_file"/>
<AppenderRef ref="warn_file"/>
<AppenderRef ref="error_file"/>
</Root>
</Loggers>
</configuration>