我试图通过springboot下的配置文件拆分我的logback.xml,这是我的方法:
的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 ch.qos.logback.core.joran.spi.Interpreter@2:16 - no applicable action for [configuration], current ElementPath is [[configuration][configuration]]
13:01:44,674 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@3:81 - no applicable action for [include], current ElementPath is [[configuration][configuration][include]]
13:01:44,674 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:89 - no applicable action for [include], current ElementPath is [[configuration][configuration][include]]
13:01:44,674 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@6:25 - no applicable action for [root], current ElementPath is [[configuration][configuration][root]]
13:01:44,674 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@7: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 :(得分:91)
Spring boot documentation建议使用logback-spring.xml
而不是logback.xml
,其中您可以使用spring profile标记:
<configuration>
<springProfile name="workspace">
...
</springProfile>
<springProfile name="dev,prd">
...
</springProfile>
</configuration>
答案 1 :(得分:8)
如果您想为不同的配置文件使用不同的logback配置文件,则可以从application-*.properties
文件中进行更改。
例如,在您的application-prod.properties
中,您可以说:
logging.config=src/main/resources/logback-prod.xml
答案 2 :(得分:3)
在logback.xml
配置文件中的替代方式,取决于Spring Profile的配置分隔看起来像:
<!-- Loggers setup according to Spring Profile-->
<springProfile name="localdev">
<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</root>
<logger name="com.myapp" level="debug" additivity="false">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</logger>
</springProfile>
<springProfile name="test">
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
<logger name="com.myapp" level="debug">
<appender-ref ref="STDOUT"/>
</logger>
</springProfile>
答案 3 :(得分:0)
包含的XML应具有顶部节点included
而不是configuration
。
答案 4 :(得分:-1)
我通过在application-dev.properties和application-prod.properties中为HOME_LOG变量设置不同的路径并在logback.spring.xml文件中设置动态资源属性来解决了这个问题
logback-spring.xml
<property resource="application-${spring.profiles.active}.properties" />
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%gray(%d{ISO8601}) %highlight(%-5level) [%yellow(%t)] %cyan(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>
<appender name="FILE-ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${HOME_LOG}/mylog.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${HOME_LOG}/backup/mylog-%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</encoder>
</appender>
和在application-dev.properties
中 HOME_LOG=C:/var/log/myapp/dev
和在application-prod.properties中
HOME_LOG=C:/var/log/myapp/prod
答案 5 :(得分:-2)
好吧,我找到了一个解决方案:
我将as属性移动到我的application.properties,并删除了logback.xml的标记根并正常工作。