RubesMN RubesMN - 1 year ago 1325
Java Question

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

I'm using .yml files and want to pull application name out of bootstrap.yml or application.yml.


<springProperty scope="context" name="myappName" source=""/>
<appender name="logFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">

The documentation here Spring Boot Logback extensions doesn't help much.

This other stackoverflow question Unable to use Spring Property Placeholders in logback.xml is older and doesnt work for me either. 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. 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".

Answer Source

To provide my analysis and a solution for future readers... I tried with values in bootstrap.yml, then application.yml, then 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 for all framework users) via new ClassPathResource("/bootstrap.yml")
  • Set a new property, in a HashMap based off the value
  • Called SpringApplication.setDefaultProperties() with that HashMap
  • Then I was able to use ${myappName} within the logback-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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download