2

I'm using SpringBoot 2.1 with the spring-boot-maven-plugin and git-commit-id-plugin to automatically populate the actuator info endpoint with the build information. Works great. I'm getting the values grouped under the json properites build and git.

Now I also want to include these information in the json formatted log messages (using logback logstash). Therefore I tried using the springProperty extension, but it seems that these elements are not available as environment entries.

<springProperty scope="context" name="info_version" source="info.build.version"/>
<springProperty scope="context" name="build_version" source="build.version"/>

I both cases nothing gets resolved. I also tried to add the build-info.properties as a property source manually by

@PropertySource("classpath:META-INF/build-info.properties")

...but this doesn't seem to work neither.

Thus, how can I include properties like the build-version, git-commit or something else from info entries in the log messages?

Leikingo
  • 890
  • 3
  • 10
  • 23
  • I have a similar propblem where I try to access the git.commit.id.abbrev defined in git.properties within logback-spring.xml (it doesn't work). I suspect this is because Logback is initialized before the loading of git.properties in the environment. If I set git.commit.id.abbrev to some arbitrary value in bootstrap.yml, then I can use it Logback (not a solution unfortunately). – Christophe L Aug 17 '19 at 05:11

1 Answers1

5

Unfortunately I don't think this is possible as you describe. As per https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html#boot-features-custom-log-configuration:

Since logging is initialized before the ApplicationContext is created, it is not possible to control logging from @PropertySources in Spring @Configuration files. The only way to change the logging system or disable it entirely is via System properties.

I believe the git.properties and build-info.properties are included in the Environment too late and their values cannot be used during Logback initialization (this also explains why @PropertySource("classpath:META-INF/build-info.properties") doesn't work either).

You might be able to inject the build info in logback-spring.xml at build time using Maven's resource filtering mechanism.

EDIT 1:

I've been able to inject the commit ID in logback-spring.xml using Maven resource filtering

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        ...
    </build>

and then in logback-spring.xml:

<encoder class="net.logstash.logback.encoder.LogstashEncoder">
    <version>@git.commit.id.abbrev@</version>
</encoder>

EDIT 2:

Another (better) solution referenced in the comment by @Leikingo is to import git.properties in the logback config file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property resource="git.properties" />
    <springProperty scope="context" name="application_name" source="spring.application.name" />
    <appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LogstashEncoder">
            <version>${git.commit.id.abbrev}</version>
        </encoder>
    </appender>
    <root level="INFO">
        <appender-ref ref="jsonConsoleAppender" />
    </root>
</configuration>
Christophe L
  • 13,725
  • 6
  • 33
  • 33
  • 3
    Great. Works like charm. I did the resource filtering in the past but totally forgot about this. Btw. I also found another way, at least for the `git.properties`. Using a regular logback properties import ``. Then you can reference the id with `${git.commit.id.abbrev}`. – Leikingo Aug 26 '19 at 14:24
  • Cool, that's even better than Maven filtering! – Christophe L Sep 28 '19 at 22:48