基于Springboot使用logback的注意事項
Springboot logback的注意事項
項目使用SpringBoot搭建的,開發(fā)環(huán)境沒有發(fā)現(xiàn)問題,日志輸出位置也正常。
項目的日志沒有使用默認(rèn)配置文件名方式,而是一個環(huán)境一套配置文件,所以日志也是通過application.properties配置中間接指定的;
比如開發(fā)環(huán)境:
application.properties文件配置為:spring.profiles.active=dev
application-dev.properties文件的日志配置:logging.config=classpath:logback-dev.xml
但是在生產(chǎn)環(huán)境的時候發(fā)現(xiàn)啟動項目會輸出多個日志文件???
通過查看SpringBoot源碼org.springframework.boot.context.logging.LoggingApplicationListener日志處理模塊,在org.springframework.boot.logging.logback.LogbackLoggingSystem對象中找到加載默認(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
看似沒有問題,其實我的配置文件中還包含一套測試環(huán)境的配置文件,剛好其中的日志配置文件名是logback-test.xml與默認(rèn)加載的配置文件名正好相同。
找到原因后將生產(chǎn)環(huán)境中的logback-test.xml刪除掉日志輸出就正常了。
結(jié)論:
如果項目中有多套環(huán)境是日志文件的名千萬不要和默認(rèn)配置文件名相同。
最后看了一下log4j的加載的源碼,貼出來看看,多套環(huán)境時避免使用:
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會遇到的坑
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包會與spring-boot-starter-web沖突,應(yīng)該是springboot中已經(jīng)包含了這個包,
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency>
去掉這個導(dǎo)入就好了。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot整合rocketmq實現(xiàn)分布式事務(wù)
大多數(shù)情況下很多公司是使用消息隊列的方式實現(xiàn)分布式事務(wù)。 本篇文章重點講解springboot環(huán)境下整合rocketmq實現(xiàn)分布式事務(wù),感興趣的可以了解一下2021-05-05SpringBoot+Redis執(zhí)行l(wèi)ua腳本的方法步驟
這篇文章主要介紹了SpringBoot+Redis執(zhí)行l(wèi)ua腳本的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Java使用modbus-master-tcp實現(xiàn)modbus tcp通訊
這篇文章主要為大家詳細(xì)介紹了另外一種Java語言的modbux tcp通訊方案,那就是modbus-master-tcp,文中的示例代碼講解詳細(xì),需要的可以了解下2023-12-12