java Logback 和 Spring Boot 的新 springProperty 查找机制不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33505624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Logback and Spring Boot's new springProperty lookup mechanism not working
提问by RubesMN
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 extensionsdoesn't help much.
Spring Boot Logback 扩展这里的文档没有多大帮助。
This other stackoverflow question Unable to use Spring Property Placeholders in logback.xmlis older and doesnt work for me either. Any insight would be helpful.
另一个 stackoverflow 问题无法在 logback.xml 中使用 Spring 属性占位符较旧,对我也不起作用。任何见解都会有所帮助。
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 myappNamedoes not get a value. 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".
对于第二次信息请求,实际发生的是 logback 属性myappName没有获得值。我知道这一点的方式是该值变为“myappName_IS_UNDEFINED”并且我的日志文件被命名为“myappName_IS_UNDEFINED.log”并且%contextName 被设置为“myappName_IS_UNDEFINED”。
回答by RubesMN
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. I thought it was because I used logback.xml
, but converting to logback-spring.xml
resulted in no change. 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. 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.
为未来的读者提供我的分析和解决方案......我尝试使用、 then 、 then 中的spring.application.name
值,但没有奏效。我以为是因为我使用了,但转换为没有改变。查看此处提交的代码,通过拉取值取决于何时加载属性源与何时解释文件。不知道为什么 Dave Syer 能够让它工作,但在将本地属性源添加到环境之前填充了我的 .xml 变量。 bootstrap.yml
application.yml
application.properties
logback.xml
logback-spring.xml
this.environment.getProperty(source)
logback-spring.xml
The value ispopulated within the .xml file if I set it via SpringApplication.setDefaultProperties(). Hence that is the route I took.
如果我通过 SpringApplication.setDefaultProperties() 设置该值,该值将填充在 .xml 文件中。所以这就是我走的路线。
- Built a SpringApplicationRunListener
- In
SpringApplicationRunListener.started()
, I read in bootstrap.yml (where I required spring.application.name for all framework users) vianew ClassPathResource("/bootstrap.yml")
- Set a new property,
service.log.name
in a HashMap based off the value - Called
SpringApplication.setDefaultProperties()
with that HashMap - Then I was able to use
${myappName}
within thelogback-spring.xml
file
- 构建了一个 SpringApplicationRunListener
- 在 中
SpringApplicationRunListener.started()
,我通过new ClassPathResource("/bootstrap.yml")
service.log.name
在基于值的 HashMap 中设置一个新属性SpringApplication.setDefaultProperties()
用那个 HashMap调用- 然后我就可以
${myappName}
在logback-spring.xml
文件中使用
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. I am open to further ideas but wanted to provide a solution that worked for others that are having the same experience.
我承认这不是一个完美的解决方案,但它目前适用,并且可能会继续适用于 springBoot 的未来版本。我对进一步的想法持开放态度,但想提供一个适用于其他有相同经历的人的解决方案。
回答by LIU ShouHai
Our solution is to rename logback(-spring).xml
to e.g. 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, e.g.:
我们的解决方案是重命名logback(-spring).xml
为 eglogback-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>
回答by Nikita Khomenko
You can simply add this to your logback file:
您可以简单地将其添加到您的 logback 文件中:
<property resource="application.properties" />
回答by Abhishek katiyar
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"/>
<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
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}
.
# Logging Configurations
logging:
config: "classpath:logback-spring.xml"
现在在 logback-spring.xml 中,appName 将使用名称设置为“HelloWorld”,您可以使用{MyApp}
.
Hope the above explanation would help.
希望上面的解释会有所帮助。
回答by nedenom
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. Setting it in just one or the other does not work. Passing it only as a -D argument does also work.
我最终发现该属性必须在两个 application.yml 和 bootstrap.yml 中设置。仅将其设置为其中之一不起作用。仅将它作为 -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配置,属性值不结转。
回答by Gemini Keith
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. meanwhile, if there are multiple spring contexts the logback settings will be reset several times for each context.
顺便说一句,请确保在EnvironmentPrepared
事件导致 logback 设置将被重置之前没有以编程方式进行的 logback 设置。同时,如果有多个 spring 上下文,logback 设置将为每个上下文重置几次。
please check these rules and make sure every step is in control.
请检查这些规则并确保每一步都在控制之中。
回答by Alessandro Dionisi
For me, it only worked using a command line property, like --property.value=asd.
对我来说,它只能使用命令行属性,例如 --property.value=asd。
回答by sivakadi
It worked for me after I have updated only in bootstrap.properties. Updates in application.properties does not work and also not needed.
我只在 bootstrap.properties 中更新后,它对我有用。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>
回答by Dave Syer
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). I don't think logback will let you set the "context" name either but YMMV.
如果我将“spring.application.name”放在“application.properties”(不是“bootstrap.properties”,因为我相信日志系统在引导程序属性尚不可用的阶段初始化)中,它对我有用。我不认为 logback 会让你设置“上下文”名称,但 YMMV。