Logback and Spring Boot's new springProperty lookup mechanism not working
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.
The value is populated within the .xml file if I set it via SpringApplication.setDefaultProperties(). Hence that is the route I took.
- 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
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.
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.:
logging:
config: classpath:logback-delayed.xml
prop-to-fill-in-logback-delayed.xml: whatever
Declaring the variable in logback-delayed.xml
<springProperty scope="context" name="localName" source="prop-to-fill-in-logback-delayed.xml"/>
Using the variable in logback-delayed.xml
<file>${localName}.log</file>
You can simply add this to your logback file:
<property resource="application.properties" />