2016-08-16 21 views
4

我設置了logback.xml來根據活動的彈簧配置文件選擇使用哪個appender。該技術完美的作品,當我運行使用的應用程序當通過Maven運行時,Logback中沒有彈簧配置文件

java -jar -Dspring.profiles.active=local path/to/target/application.war

但不是當我運行它使用Spring Boot Maven Plugin,例如

mvn spring-boot:run -Drun.profiles=local

這裏的logback.xml

<root level="INFO"> 
    <if condition='"${spring.profiles.active}".contains("local")'> 
     <then> 
      <appender-ref ref="CONSOLE"/> 
     </then> 
     <else> 
      <appender-ref ref="FILE"/> 
     </else> 
    </if> 
</root> 

我會注意到,在資料中正確顯示在應用程序本身的相關部分,處理logback.xml時只是不可用。

從IntelliJ IDE運行時也會出現此問題。

是否有另一種方法使用Maven Spring Boot Plugin使配置文件對於logback.xml解析器可見,並且它是否也可以用於IntelliJ?

回答

2

您是否嘗試過通過logback-spring擴展配置logback?

在你的情況的logback-spring.xml看起來是這樣的:

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html#boot-features-logback-extensions

<?xml version="1.0" encoding="UTF-8"?> 
<include resource="org/springframework/boot/logging/logback/base.xml"/><!-- include this config if you want to use spring-boot's built-in appenders --> 
<configuration> 
    <root level="INFO"> 
     <springProfile name="local"> 
       <appender-ref ref="CONSOLE"/> 
     </springProfile> 
     <springProfile name="otherprofile"> 
       <appender-ref ref="FILE"/> 
     </springProfile> 
    </root> 
</configuration> 

更多關於的logback彈簧擴展可用選項的信息