2015-04-03 29 views
42

我使用logback庫實現了在Spring引導項目中的日誌記錄。我想根據我的彈簧配置文件(屬性'spring.pofiles.active')加載不同的日誌記錄配置文件。我有3個文件:logback-dev.xml,logback-inte.xml和logback-prod.xml。我正在使用彈簧啓動版本1.2.2.RELEASE。spring引導,logback和logging.config屬性

正如你可以在春季啓動文檔(here)閱讀。它說:

的各種記錄系統可以通過包括在類路徑相應的庫被激活,並且進一步定製通過在類路徑的根提供合適的配置文件,或在由Spring環境中指定的位置屬性logging.config。 (但是請注意,由於在創建ApplicationContext之前初始化日誌記錄,因此無法在Spring @Configuration文件中控制從@PropertySources進行的日誌記錄。系統屬性和傳統的Spring Boot外部配置文件可以正常工作。)

於是,我就設置「logging.config」屬性在我的application.properties文件:

logging.config=classpath:/logback-${spring.profiles.active}.xml 

但是,當我開始我的申請,我logback- {}的個人資料的.xml未加載...

我認爲伐木是一個常見的問題,所有遇到使用彈簧引導的項目。我想知道我是否在正確的方向,因爲我也有其他解決方案,但我發現它們並不優雅(在logback.xml文件或命令行屬性中使用Janino進行條件解析)。

+0

您是否找到一個優雅的解決方案,以便您同時擁有多個活動配置文件? – Gevorg 2015-07-09 17:52:42

+0

檢查我的編輯2016年3月3日 – 2016-03-23 09:58:59

回答

54

我找到了一個解決方案,我明白了爲什麼spring不關心我在application.properties文件中定義的'logging.config'屬性。

解決方案,並解釋:

當初始化記錄,春天開機只查找類路徑或環境變量(見http://docs.spring.io/spring-boot/docs/0.5.0.M3/api/org/springframework/boot/context/initializer/LoggingApplicationContextInitializer.html)。

我發現的最佳解決方案是包含一個父級logback.xml文件,該文件將根據我的彈簧配置文件包含正確的日誌記錄配置文件。

logback.xml:

<configuration> 
    <include resource="logback-${spring.profiles.active}.xml"/> 
</configuration> 

logback- [資料]的.xml(在這種情況下,的logback-dev.xml):

<included> 

    <!-- put your appenders --> 
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> 
    <!-- encoders are assigned the type 
    ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> 
     <encoder> 
      <pattern>%d{ISO8601} %p %t %c{0}.%M - %m%n</pattern> 
      <charset>utf8</charset> 
     </encoder> 
    </appender> 

    <!-- put your loggers here --> 
    <logger name="org.springframework.web" additivity="false" level="INFO"> 
     <appender-ref ref="CONSOLE" /> 
    </logger> 

    <!-- put your root here --> 
    <root level="warn"> 
     <appender-ref ref="CONSOLE" /> 
    </root> 

</included> 

注: 'spring.profiles.active'必須在啓動應用程序時在命令行參數中設置。 E.G代表JVM屬性:-Dspring.profiles.active=dev

價文檔:

編輯(多個活動簡檔): 爲了噸o避免多個文件,我們可以使用需要Janino相關性的條件處理(setup here),請參閱conditional documentation。 使用此方法,我們也可以同時檢查多個活動配置文件。 E.G(我沒有測試這個解決方案,把評論,如果它不工作):

<configuration> 

    <if condition='"${spring.profiles.active}".contains("profile1")'> 
     <then> 
     <!-- do whatever you want for profile1 --> 
     </then> 
    </if> 

    <if condition='"${spring.profiles.active}".contains("profile2")'> 
     <then> 
     <!-- do whatever you want for profile2 --> 
     </then> 
    </if> 

    <!-- common config --> 

</configuration> 

爲條件處理的另一個例子見javasenior答案。

+0

這種方法對您的用例來說似乎很好,但是,我們可能會在環境中定義多個配置文件,這會導致包含中斷。即spring.profiles。活性= Profile1中PROFILE2。 mjj1409 2015-04-30 15:38:00

+0

是的,這是真的。在這種情況下,我會使用janino庫。 – 2015-04-30 15:44:06

+0

當你有多個活動配置文件的同時,一個優雅的解決方案的任何更新? – Gevorg 2015-07-09 17:52:57

21

可以處理多個配置文件的另一種方法是爲每個環境創建一個單獨的屬性文件。

application-prod.properties

logging.config=classpath:logback-prod.xml 

application-dev.properties

logging.config=classpath:logback-dev.xml 

application-local.properties

logging.config=classpath:logback-local.xml 

注意

如果你不小心,你可能最終登陸某處意外

-Dspring.profiles.active=local,dev //will use logback-dev.xml 
-Dspring.profiles.active=dev,local //will use logback-local.xml 
4

條件處理的logback與將沒有太多的logback文件的解決方案。這裏是a link和帶彈簧配置文件的示例logback配置。

<configuration> 

    <property name="LOG_LEVEL" value="INFO"/> 

     <if condition='"product".equals("${spring.profiles.active}")'> 
      <then> 
       <property name="LOG_LEVEL" value="INFO"/> 
      </then> 
      <else> 
       <property name="LOG_LEVEL" value="ERROR"/> 
      </else> 
     </if> 

     . 
     . 
     appender, logger tags etc. 
     . 
     . 

     <root level="${LOG_LEVEL}"> 
      <appender-ref ref="STDOUT"/> 
     </root> 

</configuration> 

此外,你可能需要這個

<dependency> 
    <groupId>org.codehaus.janino</groupId> 
    <artifactId>janino</artifactId> 
    <version>3.0.6</version> 
</dependency> 
8

而是添加到您的pom.xml添加單獨的logback個XML的每個配置文件或具有IF條件,我建議以下(如果您必須在個XML),便於條件處理的差異較小:

<springProfile name="dev"> 
<logger name="org.sample" level="DEBUG" /> 
</springProfile> 
<springProfile name="prod"> 
<logger name="org.sample" level="TRACE" /> 
</springProfile> 
+0

指向文檔的鏈接:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging。 html#_profile_specific_configuration – 2017-05-11 13:08:31

+0

我們仍然需要指定spring.profiles.active =模式吧? – 2018-01-16 06:11:13

3

Spring有支持的logback XML文件中一個標籤<springProperty/>的,這個標籤描述here。這意味着你可以很容易地從Spring屬性文件中添加變量,即使這個變量值是由Spring的環境/系統變量來解析的。

相關問題