Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

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".

like image 428
RubesMN Avatar asked Nov 03 '15 17:11

RubesMN


1 Answers

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>
like image 108
LIU ShouHai Avatar answered Oct 12 '22 15:10

LIU ShouHai