简体   繁体   English

Logback 和 Spring Boot 的新 springProperty 查找机制不起作用

[英]Logback and Spring Boot's new springProperty lookup mechanism not working

I'm using Spring Boot 1.3.0.RC1 through spring-cloud Brixton.M2 and have been unable to pull spring boot properties into logback.xml as implied by this feature checkin Support springProperty in logback configurations我正在通过 spring-cloud Brixton.M2​​ 使用 Spring Boot 1.3.0.RC1,并且无法将 Spring Boot 属性拉入 logback.xml,正如此功能签入所暗示的那样支持 logback 配置中的 springProperty

I'm using .yml files and want to pull application name out of bootstrap.yml or application.yml.我正在使用 .yml 文件并希望从 bootstrap.yml 或 application.yml 中提取应用程序名称。

logback-spring.xml: logback-spring.xml:

<configuration>
      <springProperty scope="context" name="myappName" source="spring.application.name"/>
      <contextName>${myappName}</contextName>
      <appender name="logFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
         <file>logs/${myappName}.log</file>
         ... 
      </appender>
      ...
</configuration>

The documentation here Spring Boot Logback extensions doesn't help much. Spring Boot Logback 扩展这里的文档没有多大帮助。

This other stackoverflow question Unable to use Spring Property Placeholders in logback.xml is older and doesnt work for me either.另一个 stackoverflow 问题无法在 logback.xml 中使用 Spring 属性占位符较旧,对我也不起作用。 Any insight would be helpful.任何见解都会有所帮助。

Per request, here is the relevant dependency tree that is being used每个请求,这里是正在使用的相关依赖树

[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.3.0.RC1:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:1.3.0.RC1:compile
[INFO] |  |  +- org.springframework.boot:spring-boot:jar:1.3.0.RC1:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:1.3.0.RC1:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.3.0.RC1:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.1.3:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.1.3:compile
[INFO] |  |  |  +- org.slf4j:jcl-over-slf4j:jar:1.7.12:compile
[INFO] |  |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.12:compile
[INFO] |  |  |  \- org.slf4j:log4j-over-slf4j:jar:1.7.12:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.16:runtime

Per 2nd request for info, what is actually going on is that logback property myappName does not get a value.根据第二次信息请求,实际发生的是 logback 属性myappName没有获得值。 The way I know this is that the value becomes "myappName_IS_UNDEFINED" and my logfile gets named "myappName_IS_UNDEFINED.log" and the %contextName is set to "myappName_IS_UNDEFINED".我知道这一点的方式是该值变为“myappName_IS_UNDEFINED”并且我的日志文件被命名为“myappName_IS_UNDEFINED.log”并且%contextName 被设置为“myappName_IS_UNDEFINED”。

To provide my analysis and a solution for future readers... I tried with spring.application.name values in bootstrap.yml , then application.yml , then application.properties but none worked.为了为未来的读者提供我的分析和解决方案......我尝试在bootstrap.yml使用spring.application.name值,然后是application.yml ,然后是application.properties但没有工作。 I thought it was because I used logback.xml , but converting to logback-spring.xml resulted in no change.我以为是因为我使用了logback.xml ,但转换为logback-spring.xml导致没有任何变化。 Looking at the code committed here , pulling the values via this.environment.getProperty(source) is dependent on when the property sources are loaded vs when the logback-spring.xml file is interpreted.查看此处提交的代码,通过this.environment.getProperty(source)提取值取决于何时加载属性源与何时解释logback-spring.xml文件。 Not sure why Dave Syer was able to get it to work but my .xml variable was populated before local property sources are added to the environment.不知道为什么 Dave Syer 能够让它工作,但是在将本地属性源添加到环境之前填充了我的 .xml 变量。

The value is populated within the .xml file if I set it via SpringApplication.setDefaultProperties().如果我通过 SpringApplication.setDefaultProperties() 设置该值,该值填充在 .xml 文件中。 Hence that is the route I took.所以这就是我走的路线。

  • Built a SpringApplicationRunListener构建了一个 SpringApplicationRunListener
  • In SpringApplicationRunListener.started() , I read in bootstrap.yml (where I required spring.application.name for all framework users) via new ClassPathResource("/bootstrap.yml")SpringApplicationRunListener.started() ,我通过new ClassPathResource("/bootstrap.yml")读取了 bootstrap.yml(我需要所有框架用户的 spring.application.name new ClassPathResource("/bootstrap.yml")
  • Set a new property, service.log.name in a HashMap based off the value根据值在 HashMap 中设置新属性service.log.name
  • Called SpringApplication.setDefaultProperties() with that HashMap使用该 HashMap 调用SpringApplication.setDefaultProperties()
  • Then I was able to use ${myappName} within the logback-spring.xml file然后我就可以在logback-spring.xml文件中使用${myappName}

I admit this is not a perfect solution, but one that works for now and will likely continue to work for future releases of springBoot.我承认这不是一个完美的解决方案,但它目前适用,并且可能会继续适用于 springBoot 的未来版本。 I am open to further ideas but wanted to provide a solution that worked for others that are having the same experience.我对进一步的想法持开放态度,但想提供一个适用于其他有相同经历的人的解决方案。

Our solution is to rename logback(-spring).xml to eg logback-delayed.xml so that it won't be read before Spring Cloud Config, and then activate it later explicitly from the config file in the Cloud Config repo, eg:我们的解决方案是将logback(-spring).xml重命名为例如logback-delayed.xml以便它不会在 Spring Cloud Config 之前被读取,然后稍后从 Cloud Config 存储库中的配置文件中显式激活它,例如:

logging:
    config: classpath:logback-delayed.xml
    prop-to-fill-in-logback-delayed.xml: whatever

Declaring the variable in logback-delayed.xml在 logback-delayed.xml 中声明变量

<springProperty scope="context" name="localName" source="prop-to-fill-in-logback-delayed.xml"/>

Using the variable in logback-delayed.xml使用 logback-delayed.xml 中的变量

<file>${localName}.log</file>

您可以简单地将其添加到您的 logback 文件中:

<property resource="application.properties" />

I'm on Spring Boot 1.3.1 and I was facing the same issue.我在 Spring Boot 1.3.1 上遇到了同样的问题。

What I finally found out was that the property must be set in BOTH application.yml AND bootstrap.yml.我最终发现该属性必须在两个 application.yml 和 bootstrap.yml 中设置。 Setting it in just one or the other does not work.仅将其设置为其中之一不起作用。 Passing it only as a -D argument does also work.仅将它作为 -D 参数传递也有效。

It's a bit of a hazzle to have a double configuration though, especially when it is configured per profile as well.但是,拥有双重配置有点麻烦,尤其是当它也按配置文件进行配置时。

Maybe the logback configuration is needed in both phases and the property value does not carry over.可能两个阶段都需要logback配置,属性值不结转。

first of all, your logback configuration file name should be suffixed with -spring , no matter the file is in xml or groovy format.首先,你的 logback 配置文件名应该以-spring为后缀,无论文件是xml还是groovy格式。

in spring, config center is preferred, then command line arguments, then local properties in bootstrap.yml and all local property files.在 spring 中,首选配置中心,然后是命令行参数,然后是bootstrap.yml本地属性和所有本地属性文件。

if there are many properties in multiple configuration files the first will effect no matter what value it is.如果多个配置文件中有很多属性,无论它是什么值,第一个都会影响。

by the way, please make sure there are no programmatically logback settings before EnvironmentPrepared event cause the logback settings will be reset.顺便说一句,请确保在EnvironmentPrepared事件导致 logback 设置将被重置之前没有以编程方式进行的 logback 设置。 meanwhile, if there are multiple spring contexts the logback settings will be reset several times for each context.同时,如果有多个 spring 上下文,logback 设置将为每个上下文重置几次。

please check these rules and make sure every step is in control.请检查这些规则并确保每一步都在控制之中。

In order to access the spring app name , you must first defined the spring property in logback-spring.xml as shown below :为了访问 spring 应用程序名称,您必须首先在 logback-spring.xml 中定义 spring 属性,如下所示:

<springProperty scope="context" name="MyApp" source="com.app.star"/>

So If you are accessing the source above then logically it must be defined in application.yml file as below :因此,如果您正在访问上面的源代码,那么逻辑上它必须在 application.yml 文件中定义如下:

com: app: star: HelloWorld

# Logging Configurations logging: config: "classpath:logback-spring.xml"


Now in logback-spring.xml, the appName would be set with name as 'HelloWorld' which you can access with {MyApp} .现在在 logback-spring.xml 中,appName 将设置为名称为“HelloWorld”,您可以使用{MyApp}访问它。

Hope the above explanation would help.希望上面的解释会有所帮助。

It works for me if I put the "spring.application.name" in "application.properties" (not "bootstrap.properties" because the logging system is initialized in a phase where the bootstrap properties are not yet available I believe).如果我将“spring.application.name”放在“application.properties”中(不是“bootstrap.properties”,因为我相信日志记录系统是在引导程序属性尚不可用的阶段初始化的),它对我有用。 I don't think logback will let you set the "context" name either but YMMV.我不认为 logback 会让你设置“上下文”名称,但 YMMV。

对我来说,它只能使用命令行属性,例如 --property.value=asd。

It worked for me after I have updated only in bootstrap.properties.我只在 bootstrap.properties 中更新后,它对我有用。 Updates in application.properties does not work and also not needed. application.properties 中的更新不起作用,也不需要。

kafka.host.name=xxxxx
kafka.host.port=9092

In Logback.xml在 Logback.xml 中

<springProperty scope="context" name="kafkaHostName" source="kafka.host.name" /> 

<springProperty scope="context" name="kafkaHostPort" source="kafka.host.port" /> <springProperty scope="context" name="kafkaHostPort" source="kafka.host.port" />

  <appender name="asyncXSPKafkaAppender" class="com.github.danielwegener.logback.kafka.KafkaAppender">
        <encoder class="com.github.danielwegener.logback.kafka.encoding.LayoutKafkaMessageEncoder">
            <layout class="ch.qos.logback.classic.PatternLayout">
                <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </layout>
        </encoder>
        <topic>logstash_logs</topic>
        <keyingStrategy class="com.github.danielwegener.logback.kafka.keying.RoundRobinKeyingStrategy" />
        <deliveryStrategy class="com.github.danielwegener.logback.kafka.delivery.AsynchronousDeliveryStrategy" />
        <producerConfig>bootstrap.servers=**${kafkaHostName}:${kafkaHostPort}**</producerConfig>
        <producerConfig>retries=2</producerConfig>
    </appender>

我遇到了同样的问题,通过将我需要的属性从application.properties移动到bootstrap.properties来解决它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM