logback.xml 在 application.yml 之前被评估 [英] logback.xml is evaluated before application.yml is

查看:40
本文介绍了logback.xml 在 application.yml 之前被评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building a web application using spring-boot 1.2.2.RELEASE.

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

I want to configure the logback output according to the value of the property "spring.profiles.active".

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

That property is defined in application.yml and lockback configuration is described in logback.xml.

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

But I found logback.xml is evaluated before application.yml is evaluated.

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

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

这是我的 Application.java.

Here is my 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 时,

When I write the logback.xml in some bad format like this,

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

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

Spring-boot reports an error which shows logback.xml is read in the constructor of SpringApplicationBuilder.

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 会报告一个错误,表明它是在方法 'run' 中读取的.

On the other hand, when I put the application.yml in some bad format by adding tab character, Spring-boot reports an error which shows it is read in the method 'run'.

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)

推荐答案

如何在 logback.xml 之前启动 SpringApplication 读取 application.yml

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

你不能.

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

The Logback configuration is read very early so that logging is available for the rest of the initialisation process. There's then a second stage where Logback is reconfigured based on some of the configuration in application.yml.

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

Logging frameworks don't understand application.yml or Spring's Environment so certain properties are copied over the system properties which you can then reference in your Logback configuration. Those properties are logging.file and logging.path which are copied to LOGGING_FILE and LOGGING_PATH respectively. See the Spring Boot documentation for some more information.

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

There's also an open issue to allow more of the logging configuration to be performed in application.yml.

这篇关于logback.xml 在 application.yml 之前被评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆