Lei*_*ngo 2 logback spring-boot spring-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"/>
Run Code Online (Sandbox Code Playgroud)
我这两种情况都没有得到解决。build-info.properties我还尝试通过手动添加作为属性源
@PropertySource("classpath:META-INF/build-info.properties")
Run Code Online (Sandbox Code Playgroud)
...但这似乎也不起作用。
因此,如何在日志消息中包含信息条目中的构建版本、git-commit 或其他内容等属性?
不幸的是,我认为这不可能像你所描述的那样。根据https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html#boot-features-custom-log-configuration:
由于日志记录是在创建 ApplicationContext 之前初始化的,因此无法从 Spring @Configuration 文件中的 @PropertySources 控制日志记录。更改日志系统或完全禁用它的唯一方法是通过系统属性。
我相信 和git.properties被build-info.properties包含在环境中太晚了,并且它们的值在 Logback 初始化期间不能使用(这也解释了为什么@PropertySource("classpath:META-INF/build-info.properties")不起作用)。
您可以logback-spring.xml使用 Maven 的资源过滤机制在构建时注入构建信息。
我已经能够logback-spring.xml使用 Maven 资源过滤注入提交 ID
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
Run Code Online (Sandbox Code Playgroud)
然后在logback-spring.xml:
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<version>@git.commit.id.abbrev@</version>
</encoder>
Run Code Online (Sandbox Code Playgroud)
git.properties@Leikingo 的评论中引用的另一个(更好的)解决方案是在 logback 配置文件中导入:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2421 次 |
| 最近记录: |