Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 使用多个配置文件配置logback_Spring Boot_Logback - Fatal编程技术网

Spring boot 使用多个配置文件配置logback

Spring boot 使用多个配置文件配置logback,spring-boot,logback,Spring Boot,Logback,我试图在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="${LO

我试图在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> 
<?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>
<?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>
我进入控制台:

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
建议使用
logback spring.xml
而不是
logback.xml
,在其中您可以使用spring profile标记:

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

...
...

如果您需要不同配置文件的不同回写配置文件,可以从
应用程序-*.properties
文件中进行更改

例如,在您的
应用程序-prod.properties
中,您可以说:

logging.config=src/main/resources/logback-prod.xml

另一种方法是在
logback.xml
config文件中,根据Spring概要文件分离配置,如下所示:

<!--    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>


包含的XML应该包含顶部节点
而不是
配置

或yml->logging.config:src/main/resources/logback-prod.xml在IDE中运行时有效,但在另一台机器上打包和执行时无效。您可以使用logging.config=classpath:logback-prod.xml在类路径()上搜索
<!--    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>