淺析Spring boot 中 logback 配置<springProperty> 讀取application.properties 中的屬性
Spring Boot 中 logback 讀取application.properties 中的屬性
其中使用的時(shí)候發(fā)現(xiàn)了一個(gè)問(wèn)題,就是如果使用的lobback配置文件的名稱是logback.xml 會(huì)先出現(xiàn)先找不到設(shè)置的屬性,然后項(xiàng)目啟動(dòng)起來(lái)才會(huì)找到,
比如:
<springProperty scope="context" name="log.path" source="logback.path" defaultValue="logs"/>
記錄日志文件存放地址屬性,會(huì)在項(xiàng)目啟動(dòng)的時(shí)候想出現(xiàn)一個(gè)找不到log.path_is_UNDIFIND 的目錄,項(xiàng)目啟動(dòng)起來(lái)后才會(huì)讀取到屬性,在配置路徑上生成日志文件。
可以通過(guò)將logback.xml 文件名稱修改為logback-spring.xml 就不會(huì)出現(xiàn)這個(gè)情況。
SpringBoot 配置文件的加載順序
logback.xml—>application.properties—>logback-spring.xml.
補(bǔ)充:Spring Boot筆記-logback-spring.xml獲取application.properties中的變量
logback.xml的加載順序早于springboot的application.yml (或application.properties) 配置文件當(dāng)然讀不到application.yml(或application.properties)文件中的值了。
需要通過(guò)springProperty標(biāo)簽來(lái)引用:<springProperty scope="context" name="log.path" source="path.log"/>這里的name和上面property標(biāo)簽。
如下:
其中application.properties:
springBoot.log=D:/logs
logback-spring.xml
<?xml version="1.0" encoding="UTF-8" ?> <configuration> <springProperty scope="context" name="LOG_HOME" source="springBoot.log" /> <!--輸出日志格式--> <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern> %d - %msg%n </pattern> </layout> </appender> <!--只保存info日志--> <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <encoder> <pattern> %d - %msg%n </pattern> </encoder> <!--滾動(dòng)輸出策略--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--路徑--> <!-- <fileNamePattern>D:/logs/info/info.%d.log</fileNamePattern>--> <fileNamePattern>${LOG_HOME}/info/info.%d.log</fileNamePattern> </rollingPolicy> </appender> <!--只保存warn日志--> <appender name="fileWarnLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>WARN</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <encoder> <pattern> %d - %msg%n </pattern> </encoder> <!--滾動(dòng)輸出策略--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--路徑--> <!-- <fileNamePattern>D:/logs/warn/warn.%d.log</fileNamePattern>--> <fileNamePattern>${LOG_HOME}/warn/warn.%d.log</fileNamePattern> </rollingPolicy> </appender> <!--只保存error日志--> <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> </filter> <encoder> <pattern> %d - %msg%n </pattern> </encoder> <!--滾動(dòng)輸出策略--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--路徑--> <!-- <fileNamePattern>D:/logs/error/error.%d.log</fileNamePattern>--> <fileNamePattern>${LOG_HOME}/error/error.%d.log</fileNamePattern> </rollingPolicy> </appender> <root level="info"> <appender-ref ref="consoleLog"/> <appender-ref ref="fileInfoLog"/> <appender-ref ref="fileWarnLog"/> <appender-ref ref="fileErrorLog"/> </root> </configuration>
到此這篇關(guān)于淺析Spring boot 中 logback 配置<springProperty> 讀取application.properties 中的屬性的文章就介紹到這了,更多相關(guān)Spring Boot 中 logback 讀取application.properties 中的屬性內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis sum(參數(shù)) 列名作為參數(shù)的問(wèn)題
這篇文章主要介紹了mybatis sum(參數(shù)) 列名作為參數(shù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Java后臺(tái)批量生產(chǎn)echarts圖表并保存圖片
這篇文章主要介紹了Java后臺(tái)批量生產(chǎn)echarts圖表并保存圖片,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05Mybatis中動(dòng)態(tài)SQL,if,where,foreach的使用教程詳解
MyBatis的動(dòng)態(tài)SQL是基于OGNL表達(dá)式的,它可以幫助我們方便的在SQL語(yǔ)句中實(shí)現(xiàn)某些邏輯。這篇文章主要介紹了Mybatis中動(dòng)態(tài)SQL,if,where,foreach的使用教程,需要的朋友可以參考下2017-11-11hibernate通過(guò)session實(shí)現(xiàn)增刪改查操作實(shí)例解析
這篇文章主要介紹了hibernate通過(guò)session實(shí)現(xiàn)增刪改查操作實(shí)例解析,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12Java實(shí)現(xiàn)企業(yè)微信消息推送功能的詳細(xì)步驟
這篇文章主要介紹了Java實(shí)現(xiàn)企業(yè)微信消息推送功能,本文圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04解析springBoot-actuator項(xiàng)目構(gòu)造中health端點(diǎn)工作原理
這篇文章主要介紹了springBoot-actuator中health端點(diǎn)工作原理,對(duì)spring-boot-actuator的項(xiàng)目構(gòu)造,工作原理進(jìn)行了全面的梳理,側(cè)重health健康檢查部分2022-02-02淺析Java如何優(yōu)雅的避免那無(wú)處不在的空指針異常
在Java編程語(yǔ)言中,NullPointerException(簡(jiǎn)稱NPE)是一種常見(jiàn)的運(yùn)行時(shí)異常,本文主要來(lái)和大家講講如何優(yōu)雅的避免這些空指針異常,需要的可以參考下2024-03-03Java手機(jī)號(hào)最新校驗(yàn)規(guī)則
在Java中,進(jìn)行手機(jī)號(hào)校驗(yàn)通常使用正則表達(dá)式(Regex)來(lái)匹配手機(jī)號(hào)的格式,以下是一個(gè)基于當(dāng)前(截至2024年)中國(guó)手機(jī)號(hào)規(guī)則的校驗(yàn)方法,感興趣的朋友跟隨小編一起看看吧2024-05-05