2016-12-01 412 views
0

目前我使用maven這樣使用Spring @profile加載log4j2 xml配置,而不是行家<profile>

<profiles> 
    <profile> 
     <id>dev</id> 
     <activation> 
      <activeByDefault>true</activeByDefault> 
     </activation> 
     <build> 
      <resources> 
       <resource> 
        <directory>src/main/resources/env-dev</directory> 
       </resource> 
      </resources> 
     </build> 
    </profile> 
    <profile> 
     <id>test</id> 
     <build> 
      <resources> 
       <resource> 
        <directory>src/main/resources/env-test</directory> 
       </resource> 
      </resources> 
     </build> 
    </profile> 
    <profile> 
     <id>prd</id> 
     <build> 
      <resources> 
       <resource> 
        <directory>src/main/resources/env-prd</directory> 
       </resource> 
      </resources> 
     </build> 
    </profile> 
</profiles> 

enter image description here

加載我log4j2配置但這需要我建立每個新戰爭環境,我想避免。有沒有辦法讓我使用Spring @Profile註釋來加載log4j2配置?

回答

2

我還沒有嘗試過log4j2,但我相信你可以使用屬性logging.config提供配置文件位置。

作爲一個例子: -

application-dev.properties文件代替下面的內容。此配置文件可以通過使用-Dspring.profiles.active = dev的

logging.config=/env-test/log4j2.xml

application-prd.properties文件代替以下內容被激活。此配置文件可以通過使用-Dspring.profiles.active = PRD

logging.config=/env-prd/log4j2.xml

請都src中的上述配置文件/主/資源目錄被激活。

注意:如果使用YAML更方便,因爲它們可以放置在單個配置文件中,使用---分隔文檔。