基于Springboot使用logback的注意事項(xiàng)
Springboot logback的注意事項(xiàng)
項(xiàng)目使用SpringBoot搭建的,開(kāi)發(fā)環(huán)境沒(méi)有發(fā)現(xiàn)問(wèn)題,日志輸出位置也正常。
項(xiàng)目的日志沒(méi)有使用默認(rèn)配置文件名方式,而是一個(gè)環(huán)境一套配置文件,所以日志也是通過(guò)application.properties配置中間接指定的;
比如開(kāi)發(fā)環(huán)境:
application.properties文件配置為:spring.profiles.active=dev
application-dev.properties文件的日志配置:logging.config=classpath:logback-dev.xml
但是在生產(chǎn)環(huán)境的時(shí)候發(fā)現(xiàn)啟動(dòng)項(xiàng)目會(huì)輸出多個(gè)日志文件???
通過(guò)查看SpringBoot源碼org.springframework.boot.context.logging.LoggingApplicationListener日志處理模塊,在org.springframework.boot.logging.logback.LogbackLoggingSystem對(duì)象中找到加載默認(rèn)配置文件的代碼:
@Override protected String[] getStandardConfigLocations() { return new String[] { "logback-test.groovy", "logback-test.xml", "logback.groovy", "logback.xml" }; }
生產(chǎn)配置為:
application.properties文件配置為:spring.profiles.active=prod
application-prod.properties文件的日志配置:logging.config=classpath:logback-prod.xml
看似沒(méi)有問(wèn)題,其實(shí)我的配置文件中還包含一套測(cè)試環(huán)境的配置文件,剛好其中的日志配置文件名是logback-test.xml與默認(rèn)加載的配置文件名正好相同。
找到原因后將生產(chǎn)環(huán)境中的logback-test.xml刪除掉日志輸出就正常了。
結(jié)論:
如果項(xiàng)目中有多套環(huán)境是日志文件的名千萬(wàn)不要和默認(rèn)配置文件名相同。
最后看了一下log4j的加載的源碼,貼出來(lái)看看,多套環(huán)境時(shí)避免使用:
private String[] getCurrentlySupportedConfigLocations() { List<String> supportedConfigLocations = new ArrayList<>(); supportedConfigLocations.add("log4j2.properties"); if (isClassAvailable("com.fasterxml.jackson.dataformat.yaml.YAMLParser")) { Collections.addAll(supportedConfigLocations, "log4j2.yaml", "log4j2.yml"); } if (isClassAvailable("com.fasterxml.jackson.databind.ObjectMapper")) { Collections.addAll(supportedConfigLocations, "log4j2.json", "log4j2.jsn"); } supportedConfigLocations.add("log4j2.xml"); return StringUtils.toStringArray(supportedConfigLocations); }
springboot使用logback會(huì)遇到的坑
Caused by: java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.apache.logging.slf4j.Log4jLoggerFactory loaded from file:/C:/Users/fyk/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.7/log4j-slf4j-impl-2.7.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.apache.logging.slf4j.Log4jLoggerFactory at org.springframework.util.Assert.instanceCheckFailed(Assert.java:389) at org.springframework.util.Assert.isInstanceOf(Assert.java:327) at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:274) at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:98) at org.springframework.boot.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java:230) at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:209) at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122) at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:69) at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:48) at org.springframework.boot.SpringApplication.run(SpringApplication.java:292) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) at com.light.SpringbootApplication.main(SpringbootApplication.java:32) ... 5 more
在spring boot中導(dǎo)入logback jar包會(huì)與spring-boot-starter-web沖突,應(yīng)該是springboot中已經(jīng)包含了這個(gè)包,
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency>
去掉這個(gè)導(dǎo)入就好了。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot整合rocketmq實(shí)現(xiàn)分布式事務(wù)
大多數(shù)情況下很多公司是使用消息隊(duì)列的方式實(shí)現(xiàn)分布式事務(wù)。 本篇文章重點(diǎn)講解springboot環(huán)境下整合rocketmq實(shí)現(xiàn)分布式事務(wù),感興趣的可以了解一下2021-05-05Java如何獲取Json中的數(shù)據(jù)實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Java如何獲取Json中數(shù)據(jù)的相關(guān)資料,我們?cè)谌粘i_(kāi)發(fā)中少不了和JSON數(shù)據(jù)打交道,那么我們來(lái)看看JAVA中常用的JSON獲取方式,需要的朋友可以參考下2023-09-09SpringBoot+Redis執(zhí)行l(wèi)ua腳本的方法步驟
這篇文章主要介紹了SpringBoot+Redis執(zhí)行l(wèi)ua腳本的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Java使用modbus-master-tcp實(shí)現(xiàn)modbus tcp通訊
這篇文章主要為大家詳細(xì)介紹了另外一種Java語(yǔ)言的modbux tcp通訊方案,那就是modbus-master-tcp,文中的示例代碼講解詳細(xì),需要的可以了解下2023-12-12使用Mybatis如何實(shí)現(xiàn)多個(gè)控制條件查詢
這篇文章主要介紹了使用Mybatis如何實(shí)現(xiàn)多個(gè)控制條件查詢,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03