2016-08-16 23 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弹簧扩展可用选项的信息