Spring-引导在 Logback 中将构建信息包含为 Spring属性
Spring-Boot include build-info as SpringProperty in Logback
我将 SpringBoot 2.1 与 spring-boot-maven-plugin
和 git-commit-id-plugin
一起使用,以使用构建信息自动填充执行器信息端点。效果很好。我得到的值分组在 json 属性 build
和 git
.
下
现在我还想将这些信息包含在 json 格式的日志消息中(使用 logback logstash)。因此我尝试使用 springProperty
扩展,但这些元素似乎不能用作环境条目。
<springProperty scope="context" name="info_version" source="info.build.version"/>
<springProperty scope="context" name="build_version" source="build.version"/>
我这两种情况都没有解决。我还尝试通过
手动将 build-info.properties
添加为 属性 来源
@PropertySource("classpath:META-INF/build-info.properties")
...但这似乎也不起作用。
因此,如何在日志消息的信息条目中包含构建版本、git-提交或其他内容等属性?
很遗憾,我认为这不可能像您描述的那样。根据 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.
我认为 git.properties
和 build-info.properties
太晚包含在环境中,并且在 Logback 初始化期间不能使用它们的值(这也解释了为什么 @PropertySource("classpath:META-INF/build-info.properties")
也不起作用).
您可以在构建时使用 Maven 的资源过滤机制在 logback-spring.xml
中注入构建信息。
编辑 1:
我已经能够使用 Maven 资源过滤
在 logback-spring.xml
中注入提交 ID
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
然后在 logback-spring.xml
:
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<version>@git.commit.id.abbrev@</version>
</encoder>
编辑 2:
@Leikingo 在评论中引用的另一个(更好的)解决方案是在 logback 配置文件中导入 git.properties
:
<?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>
我将 SpringBoot 2.1 与 spring-boot-maven-plugin
和 git-commit-id-plugin
一起使用,以使用构建信息自动填充执行器信息端点。效果很好。我得到的值分组在 json 属性 build
和 git
.
现在我还想将这些信息包含在 json 格式的日志消息中(使用 logback logstash)。因此我尝试使用 springProperty
扩展,但这些元素似乎不能用作环境条目。
<springProperty scope="context" name="info_version" source="info.build.version"/>
<springProperty scope="context" name="build_version" source="build.version"/>
我这两种情况都没有解决。我还尝试通过
手动将build-info.properties
添加为 属性 来源
@PropertySource("classpath:META-INF/build-info.properties")
...但这似乎也不起作用。
因此,如何在日志消息的信息条目中包含构建版本、git-提交或其他内容等属性?
很遗憾,我认为这不可能像您描述的那样。根据 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.
我认为 git.properties
和 build-info.properties
太晚包含在环境中,并且在 Logback 初始化期间不能使用它们的值(这也解释了为什么 @PropertySource("classpath:META-INF/build-info.properties")
也不起作用).
您可以在构建时使用 Maven 的资源过滤机制在 logback-spring.xml
中注入构建信息。
编辑 1:
我已经能够使用 Maven 资源过滤
在logback-spring.xml
中注入提交 ID
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
然后在 logback-spring.xml
:
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<version>@git.commit.id.abbrev@</version>
</encoder>
编辑 2:
@Leikingo 在评论中引用的另一个(更好的)解决方案是在 logback 配置文件中导入 git.properties
:
<?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>