2016-02-26 556 views
12

我正嘗試通過曲線下springboot分裂我logback.xml,這是我的方法:配置的logback使用多個配置文件

的logback-prod.xml

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
<include resource="org/springframework/boot/logging/logback/defaults.xml" /> 
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:- ${java.io.tmpdir:-/tmp}}/}spring.log}"/> 
<include resource="org/springframework/boot/logging/logback/file- appender.xml" /> 

<root level="INFO"> 
    <appender-ref ref="CONSOLE" /> 
</root> 
</configuration> 

的logback-dev的。 XML

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
<include resource="org/springframework/boot/logging/logback/defaults.xml" /> 
<include resource="org/springframework/boot/logging/logback/console-appender.xml" /> 

<root level="DEBUG"> 
    <appender-ref ref="CONSOLE" /> 
</root> 

logback.xml

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
<include resource="logback-${spring.profiles.active}.xml"/> 

<root level="INFO"> 
    <appender-ref ref="FILE" /> 
    <appender-ref ref="CONSOLE" /> 
</root> 

最後使用:

-Dspring.profiles.active=dev 
or 
-Dspring.profiles.active=prod 

我在控制檯有:

13:01:44,673 |-ERROR in [email protected]:16 - no applicable action for [configuration], current ElementPath is [[configuration][configuration]] 
13:01:44,674 |-ERROR in [email protected]:81 - no applicable action for [include], current ElementPath is [[configuration][configuration][include]] 
13:01:44,674 |-ERROR in [email protected]:89 - no applicable action for [include], current ElementPath is [[configuration][configuration][include]] 
13:01:44,674 |-ERROR in [email protected]:25 - no applicable action for [root], current ElementPath is [[configuration][configuration][root]] 
13:01:44,674 |-ERROR in [email protected]:39 - no applicable action for [appender-ref], current ElementPath is [[configuration][configuration][root][appender-ref]] 
13:01:44,675 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO 

回答

0

嗯,我找到了一個解決方案:

我將as屬性移動到我的application.properties中,並且還刪除了logback.xml的標記根並正常工作。

+0

你是如何管理不同的環境不同的logback.xml。我想爲每個環境/配置文件分別提供logback.xml文件? – Sachin

40

Spring boot documentation建議使用logback-spring.xml而非logback.xml,並在其中你可以使用Spring配置文件標籤:

<configuration> 
    <springProfile name="workspace"> 
    ... 
    </springProfile> 
    <springProfile name="dev,prd"> 
    ... 
    </springProfile> 
</configuration>