首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将git提交id输出到使用Spring的所有logback日志?

如何将git提交id输出到使用Spring的所有logback日志?
EN

Stack Overflow用户
提问于 2020-02-08 16:36:27
回答 1查看 1.3K关注 0票数 3

我使用SpringBoot和Logback以及git提交id maven插件。如何将logback和SpringBoot配置为在日志输出中包含执行器返回的git id?我希望每一行输出都包含生成输出行的引导应用程序的git提交id。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-10 01:53:34

默认情况下,git-commit-id-plugin在类路径根目录中创建一个名为git.properties的文件,其中包含许多属性,包括git.commit.id

您可以在logback的配置文件中访问导入这些属性,并在logback配置中的任何位置访问属性。

例如,您可以在如下模式中使用它:

代码语言:javascript
复制
<configuration>

    <!-- Expose the properties from the file generated by git-commit-id-plugin -->
    <property resource="git.properties"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} ${git.commit.id} - %message%n</pattern>
        </encoder>
    </appender>
    ...
</configuration>

或者(因为您用logstash-logback-encoder标记了这个问题),您可以通过自定义字段或通过模式提供者将其包括在JSON字段中,如下所示:

代码语言:javascript
复制
<configuration>

    <!-- Expose the properties from the file generated by git-commit-id-plugin -->
    <property resource="git.properties"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers>
                <pattern>
                    <pattern>{"commitId":"${git.commit.id}"}</pattern>
                </pattern>
            ...
            </providers>
        </encoder>
    </appender>
    ...
</configuration>
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60128839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档