2

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

vicsz
  • 9,552
  • 16
  • 69
  • 101

4 Answers4

7

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

Vipul Panth
  • 5,221
  • 4
  • 16
  • 27
  • Tried that approach (already processing .properties files, added xml) .. The problem is that I want to inject the spring application name in addition to version . So I have in. the logback file and ${APP_NAME} ${version}. .. During process resources Gradle will try to replace all ${} fields however the process will fail due to APP_NAME not existing for gradle – vicsz Feb 05 '17 at 12:10
1

If you already managed to instruct Gradle to set version in the manifest file, then you could try to instruct gradle to replace ${version} in your logback.xml

I do not know Gradle, but Maven, so in Maven: it is called filtered resources, an I think it is quite the same in Gradle: https://dzone.com/articles/resource-filtering-gradle

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • Don't believe it will work when I'm also trying inject SpringProperty of my application name (it's different from the jar file) .. ${APP_NAME} ${version} <-- in logback will fail as APP_NAME doesn't exist during processResources – vicsz Feb 05 '17 at 12:09
  • Figured it out .... it's smelly ... but I use springProperty in Logback to inject the actual Spring Boot Application name (not the sub jar that contain the logback file), I use processResources, and I escape the ${APP_NAME) with /$ so that processResoruce doesn't try to populate it – vicsz Feb 05 '17 at 12:21
1
  1. Added git-commit-id-plugin plugin in pom.xml. This creates git.properties at build directory and updates META-INF with build-info.properties.

    <plugin>
        <groupId>pl.project13.maven</groupId>
        <artifactId>git-commit-id-plugin</artifactId>
        <version>4.0.0</version>
        <executions>
            <execution>
                <id>get-the-git-infos</id>
                <goals>
                    <goal>revision</goal>
                </goals>
                <phase>initialize</phase>
            </execution>
            <execution>
                <id>validate-the-git-infos</id>
                <goals>
                    <goal>validateRevision</goal>
                </goals>
                <phase>package</phase>
            </execution>
        </executions>
        <configuration>
            <failOnNoGitDirectory>false</failOnNoGitDirectory>
            <generateGitPropertiesFile>true</generateGitPropertiesFile>
            <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
        </configuration>
    </plugin>
    
  2. Added MEATA-INF/build-info.properties as resource in logback.xml and used the variable ${build.version} to log the version.

    <property resource="META-INF/build-info.properties" />
    <property name="LOG_PATTERN" value="%level{length=1} %d{yyyy-MM-dd HH:mm:ss.SSS} | buildNumber=${build.version} | UserAgent = %X{User-Agent} | thread=%t | class=%c-%L | message=%msg%n" />
    
Paramesh Korrakuti
  • 1,997
  • 4
  • 27
  • 39
0

You can try <%=version%> to version from gradle in application.property.

For example, you have a property version in application.property

version=<%=version%>

Then the source could be 'version' in logback config.