Search code examples
springspring-bootlogback

Logback config -- how to include Spring Application Version


For analysis purposes, I want to log the application version in all log entries (ideally I would want to do this by editing the logback-spring.xml file instead of writing any Java code).

I'm am already logging spring application name successfully.

Note example startup up log message that shows correct application version.

As per my grade build -- am I updating the manifest file with the correct implementation-version. For Spring build actuator purposes I'm also setting info.build.version=${version}.

See below example -- I'm not sure what to put in ??? to correctly log the application version. I'm tried a number of keys including: info.build.version, application.version, spring.application.version, etc ..

 <springProperty name="APP_NAME" source="spring.application.name"/>

 <springProperty name="APP_VERSION" source="???"/>

 <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
     <encoder>
         <pattern> version=${APP_VERISON} site=${APP_NAME}  %msg%n </pattern>
     </encoder>
 </appender>

Starting Application v1.0.0-SNAPSHOT with PID 14147


Solution

  • According to the documentation

    For Maven you can automatically expand properties from the Maven project using resource filtering. If you use spring-boot-starter-parent you can then refer to your Maven ‘project properties’ via @..@ placeholders

    You can add maven project properties in your application.properties file such as

    app.project.version=@project.version@
    

    For Gradle you need to expand properties from the Gradle project by configuring the Java plugin’s processResources

    processResources {
        filesMatching('application.properties') {
            expand(project.properties)
        }    
    }
    

    Can access through placeholders.

    app.name=${name}
    app.description=${description}
    app.version=${version}
    

    You can use these properties to add in logback.xml

    Thanks