Spring Boot與日志SLF4J的操作方法,針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
創(chuàng)新互聯(lián)公司為您提適合企業(yè)的網(wǎng)站設(shè)計?讓您的網(wǎng)站在搜索引擎具有高度排名,讓您的網(wǎng)站具備超強的網(wǎng)絡(luò)競爭力!結(jié)合企業(yè)自身,進行網(wǎng)站設(shè)計及把握,最后結(jié)合企業(yè)文化和具體宗旨等,才能創(chuàng)作出一份性化解決方案。從網(wǎng)站策劃到做網(wǎng)站、網(wǎng)站制作, 我們的網(wǎng)頁設(shè)計師為您提供的解決方案。
市面上的日志框架:
| 日志門面(日志的抽象層) | 日志實現(xiàn) |
|---|---|
| JCL(Jakarta Commons Logging)SLF4J(Simple Logging Facade for Java)Jboss-logging | Log4jJUL(java.util.logging) Log4j2 Logback |
左邊選一個門面,右邊選一個實現(xiàn)(加粗的是同一人所寫,Logback比Log4j更加新)
日志門面:SLF4J
日志實現(xiàn):Logback
Spring Boot:底層使用Spring框架,Spring框架默認使用JCL;
Spring Boot選用SLF4J和Logback;
以后開發(fā)的時候,日志方法的調(diào)用,不應(yīng)該直接調(diào)用日志的實現(xiàn)類,而是應(yīng)該調(diào)用日志抽象層的方法
應(yīng)該給系統(tǒng)中導(dǎo)入slf4j的jar包和logback的實現(xiàn)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!");
}
}public static Logger getLogger(String name) {
ILoggerFactory iLoggerFactory = getILoggerFactory();
return iLoggerFactory.getLogger(name);
}
public static Logger getLogger(Class<?> clazz) {
Logger logger = getLogger(clazz.getName());
if (DETECT_LOGGER_NAME_MISMATCH) {
Class<?> autoComputedCallingClass = Util.getCallingClass();
if (autoComputedCallingClass != null && nonMatchingClasses(clazz, autoComputedCallingClass)) {
Util.report(String.format("Detected logger name mismatch. Given name: \"%s\"; computed name: \"%s\".", logger.getName(), autoComputedCallingClass.getName()));
Util.report("See http://www.slf4j.org/codes.html#loggerNameMismatch for an explanation");
}
}
return logger;
}圖示:

每一個日志框架都有自己的配置文件格式,使用slf4j之后,仍然需要使用各日志實現(xiàn)框架的配置文件進行配置
同一個項目中使用了很多框架,各框架使用的日志實現(xiàn)肯定會出現(xiàn)不同,如何統(tǒng)一使用slf4j + logback統(tǒng)一進行日志輸出呢?
將系統(tǒng)中其他日志框架先排除出去
用中間包來替換原有的日志框架
導(dǎo)入slf4j的其他的實現(xiàn)
slf4j" title="log4j --> slf4j">

總結(jié):
spring boot底層也使用slf4j + logback的方式實現(xiàn)
spring boot把其他日志都替換成了slf4j
中間替換包
轉(zhuǎn)換示例:

如果我們要引入其他框架,我們一定要排除原來的框架
SpringBoot默認幫我們配置好了日志模塊;
SpringBoot日志記錄的調(diào)用方式:
//記錄器
Logger logger = LoggerFactory.getLogger(SpringBoot03LoggingApplication.class);
@Test
public void contextLoads() {
//System.out.println("");
//日志的級別,級別由低到高trace<debug<info<warn<error
//可以調(diào)整輸出的日志級別,日志只會輸出當(dāng)前級別及更高級別的日志
logger.trace("這是trace日志...");
logger.debug("這是debug日志...");
logger.info("這是info日志...");//SpringBoot默認日志級別為info(root級別),沒有指定日志級別的,默認使用root級別
logger.warn("這是warn日志...");
logger.error("這是error日志...");
}SpringBoot修改日志的默認配置:
#日志級別配置項,可以指定到具體的包或者類
logging.level.com.qiang.springboot=debug
#日志文件名,如不指定路徑,則默認生成在當(dāng)前項目根目錄下
#也可以指定具體的生成路徑
#logging.file=springboot.log
#logging.file=F:/WorkspaceIDEA/logs/springboot.log
#日志生成路徑,在指定路徑下生成日志文件,文件默認名稱為spring.log
#此設(shè)置和logging.file沖突,如果二者同時出現(xiàn),則logging.file生效
logging.path=F:/WorkspaceIDEA/logs
#日志輸出格式
#console指控制臺的日志格式,file指日志文件的日志格式
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss.SSS} | %thread | %-5level | %logger{50} | %msg%logging.file | logging.path | Example | Description |
|---|---|---|---|
| (none) | (none) | Console only logging. | |
| Specific file | (none) | my.log | Writes to the specified log file. Names can be an exact location or relative to the current directory. |
| (none) | Specific directory | /var/log | Writes spring.log to the specified directory. Names can be an exact location or relative to the current directory. |
在類路徑下放每個日志框架自己的配置文件即可;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 |
SpringBoot推薦使用logback-spring.xml進行配置**,因為:
logback.xml 直接就被日志框架識別了,無法使用更多的擴展特性
logback-spring.xml:日志框架就無法識別此配置文件,而由SpringBoot框架解析配置,這樣就可以使用SpringBoot的高級特性了,如springProfile特性
<springProfile name="staging"> <!-- configuration to be enabled when the "staging" profile is active --> <!-- 可以指定某段配置只在某個環(huán)境下生效,類似yml配置文件中的段 --> </springProfile> <springProfile name="dev | staging"> <!-- configuration to be enabled when the "dev" or "staging" profiles are active --> </springProfile> <springProfile name="!production"> <!-- configuration to be enabled when the "production" profile is not active --> </springProfile>
可以根據(jù)slf4j的日志適配圖,進行相關(guān)的切換
另外,如果想使用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>
關(guān)于Spring Boot與日志SLF4J的操作方法問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
文章名稱:SpringBoot與日志SLF4J的操作方法
分享路徑:http://www.chinadenli.net/article24/gpdice.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、微信公眾號、面包屑導(dǎo)航、、商城網(wǎng)站、用戶體驗
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)