gpt4 book ai didi

spring-boot - logback.xml 在 application.yml 之前被评估

转载 作者:行者123 更新时间:2023-12-04 02:33:15 28 4
gpt4 key购买 nike

我正在使用 spring-boot 1.2.2.RELEASE 构建一个 Web 应用程序。

我想根据属性“spring.profiles.active”的值配置 logback 输出。

该属性在 application.yml 中定义,锁定配置在 logback.xml 中描述。

但我发现 logback.xml 在评估 application.yml 之前被评估。

如何在 logback.xml 之前启动一个读取 application.yml 的 SpringApplication?

这是我的 Application.java。

/**
* Application.java --
*/
package my.project;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).run(args);
}

}

当我以这样的错误格式编写 logback.xml 时,

<?xml version="1.0" encoding="UTF-8"?>
aaa

Spring-boot报错,在SpringApplicationBuilder的构造函数中读取了logback.xml。

ERROR in ch.qos.logback.core.joran.event.SaxEventRecorder@449b193b - XML_PARSING - Parsing fatal error on line 2 and column 1 org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 1;
[snip]
at at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:155)
at at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:132)
at at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:275)
at at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:160)
at at org.springframework.boot.builder.SpringApplicationBuilder.createSpringApplication(SpringApplicationBuilder.java:96)
at at org.springframework.boot.builder.SpringApplicationBuilder.<init>(SpringApplicationBuilder.java:84)
at at my.project.Application.main(Application.java:13)
[snip]

另一方面,当我通过添加制表符将 application.yml 设置为某种错误格式时,Spring-boot 报告一个错误,表明它是在方法“运行”中读取的。

Caused by: while scanning for the next token
found character '\t(TAB)' that cannot start any token. (Do not use \t(TAB) for indentation)
in 'reader', line 1, column 1:
[snip]
at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEvent(ConfigFileApplicationListener.java:126)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:151)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:128)
at org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:100)
at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:59)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:285)
at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:139)
at my.project.Application.main(Application.java:13)

最佳答案

How can I start a SpringApplication reading application.yml before logback.xml

你不能。

很早就读取了 Logback 配置,以便日志记录可用于初始化过程的其余部分。然后是第二阶段,根据 application.yml 中的一些配置重新配置 Logback。

日志记录框架不理解 application.yml 或 Spring 的环境,因此某些属性被复制到系统属性上,然后您可以在 Logback 配置中引用这些属性。这些属性是 logging.filelogging.path,分别复制到 LOGGING_FILELOGGING_PATH。查看Spring Boot documentation了解更多信息。

还有一个 open issue允许在 application.yml 中执行更多的日志记录配置。

关于spring-boot - logback.xml 在 application.yml 之前被评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29669049/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com