使用多个配置文件配置 logback [英] Configure logback using several profiles
本文介绍了使用多个配置文件配置 logback的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过 springboot 下的配置文件拆分我的 logback.xml,这是我的方法:
logback-prod.xml
logback-dev.xml
logback.xml
最后使用:
-Dspring.profiles.active=dev或者-Dspring.profiles.active=prod
我进入了控制台:
13:01:44,673 |- ch.qos.logback.core.joran.spi.Interpreter@2:16 中的错误 - [配置] 没有适用的操作,当前 ElementPath 是 [[配置][配置]]]13:01:44,674 |-ch.qos.logback.core.joran.spi.Interpreter@3:81 中的错误 - [include] 没有适用的操作,当前 ElementPath 是 [[configuration][configuration][include]]13:01:44,674 |-ch.qos.logback.core.joran.spi.Interpreter@4:89 中的错误 - [include] 没有适用的操作,当前 ElementPath 是 [[configuration][configuration][include]]13:01:44,674 |-ch.qos.logback.core.joran.spi.Interpreter@6:25 中的错误 - [root] 没有适用的操作,当前 ElementPath 是 [[configuration][configuration][root]]13:01:44,674 |-ch.qos.logback.core.joran.spi.Interpreter@7:39 中的错误 - [appender-ref] 没有适用的操作,当前 ElementPath 是 [[configuration][configuration][root][附加引用]]13:01:44,675 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - 将 ROOT 记录器的级别设置为 INFO
解决方案
Spring 启动文档 建议使用 logback-spring.xml
而不是 logback.xml
并在其中您可以使用 spring 配置文件标记:
I´m trying to split my logback.xml by profiles under springboot, this my approach:
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>
And finally use:
-Dspring.profiles.active=dev
or
-Dspring.profiles.active=prod
I got in console:
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
解决方案
Spring boot documentation recommends to use logback-spring.xml
rather than logback.xml
and in it you can use spring profile tag:
<configuration>
<springProfile name="workspace">
...
</springProfile>
<springProfile name="dev,prd">
...
</springProfile>
</configuration>
这篇关于使用多个配置文件配置 logback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文