SpringBoot中logback.xml使用application.yml中屬性

來源:互聯網
上載者:User

標籤:class   gpo   qos   tap   設定   console   使用方法   framework   logger   

教你如何使用 springProfilespringProperty 讓你的logback.xml 配置顯得更有逼格,當別人還在苦苦掙紮弄logback-{profile}.xml的時候 你一個檔案就搞定了…

- springProfile

<springProfile> 標籤允許我們更加靈活設定檔,可選地包含或排除配置部分。元素中的任何位置均支援輪廓部分。使用該name屬性指定哪個設定檔接受配置。可以使用逗號分隔列表指定多個設定檔。

<springProfile name="dev">    <!-- 開發環境時啟用 --></springProfile><springProfile name="dev,test">    <!-- 開發,測試的時候啟用--></springProfile><springProfile name="!prod">    <!-- 當 "生產" 環境時,該配置不啟用--></springProfile>
- 案例
<!-- 開發環境記錄層級為DEBUG --><springProfile name="dev">    <root level="DEBUG">        <appender-ref ref="FILE"/>        <appender-ref ref="STDOUT"/>    </root></springProfile><!-- 測試環境記錄層級為INFO --><springProfile name="test">    <root level="INFO">        <appender-ref ref="FILE"/>        <appender-ref ref="STDOUT"/>    </root></springProfile>
- springProperty

1.該 <springProperty> 標籤允許我們從Spring中顯示內容,Environment 以便在Logback中使用。如果你想將 application.properties在回讀配置中訪問檔案中的值,這將非常有用

2.標籤的工作方式與Logback的標準 <property> 標籤類似,但不是直接value 指定source屬性(從Environment)指定。scope 如果需要將屬性儲存區在local範圍之外的其他位置,則可以使用該屬性。如果您需要一個後備值,以防該屬性未設定,則Environment可以使用該defaultValue屬性。

<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host" defaultValue="localhost"/><appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender">    <remoteHost>${fluentHost}</remoteHost>    </appender>
- 案例
<!-- 讀取spring.application.name中的屬性來組建記錄檔檔案名稱 --><springProperty scope="context" name="logName" source="spring.application.name" defaultValue="localhost.log"/><appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">    <file>logs/${logName}.log</file>    <!-- 使用方法 -->    <append>true</append>    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">        <fileNamePattern>logs/${logName}-%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>        <maxFileSize>100MB</maxFileSize>        <maxHistory>7</maxHistory>        <totalSizeCap>3GB</totalSizeCap>    </rollingPolicy>    <encoder>        <pattern>[%date{yyyy-MM-dd HH:mm:ss}] [%-5level] [%logger:%line] &#45;&#45;%mdc{client} %msg%n</pattern>    </encoder>    <filter class="ch.qos.logback.classic.filter.LevelFilter">        <level>DEBUG</level>    </filter></appender>

將RelaxedPropertyResolver用於訪問環境屬性。如果使用虛線符號指定source(my-property-name)所有的變化都會被嘗試(myPropertyName,MY_PROPERTY_NAME等)。

- logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?><configuration scan="true">    <springProperty scope="context" name="logName" source="spring.application.name" defaultValue="localhost.log"/>    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">        <file>logs/${logName}.log</file>        <append>true</append>        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">            <fileNamePattern>logs/${logName}-%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>            <maxFileSize>100MB</maxFileSize>            <maxHistory>7</maxHistory>            <totalSizeCap>3GB</totalSizeCap>        </rollingPolicy>        <encoder>            <pattern>[%date{yyyy-MM-dd HH:mm:ss}] [%-5level] [%logger:%line] &#45;&#45;%mdc{client} %msg%n</pattern>        </encoder>        <filter class="ch.qos.logback.classic.filter.LevelFilter">            <level>DEBUG</level>        </filter>    </appender>    <!-- Console output -->    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">        <encoder>            <pattern>                [ %-5level] [%date{yyyy-MM-dd HH:mm:ss}] %logger{96} [%line] - %msg%n            </pattern>            <charset>UTF-8</charset> <!-- 此處設定字元集 -->        </encoder>        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">            <level>DEBUG</level>        </filter>    </appender>    <springProfile name="dev,test">        <root level="DEBUG">            <appender-ref ref="FILE"/>            <appender-ref ref="STDOUT"/>        </root>    </springProfile>    <springProfile name="prod">        <root level="INFO">            <appender-ref ref="FILE"/>            <appender-ref ref="STDOUT"/>        </root>    </springProfile>    <logger name="org.springframework" level="INFO"/>    <logger name="com.netflix" level="WARN"/>    <logger name="org" level="INFO"/>    <logger name="springfox.documentation" level="INFO"/></configuration>

 

SpringBoot中logback.xml使用application.yml中屬性

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.