Logback config — how to include Spring Application Version

喜你入骨 提交于 2019-12-24 08:26:32

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/42049723/logback-config-how-to-include-spring-application-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!