简体   繁体   English

如何使用 Spring Boot 将 git commit id 输出到所有 logback 日志?

[英]How to output git commit id to all logback logs with Spring Boot?

I am using SpringBoot with Logback along with the git commit id maven plugin.我正在使用带有 Logback 的 SpringBoot 以及 git commit id maven 插件。 How do I configure logback and SpringBoot to include the git commit id that the actuator returns in my log output?如何配置 logback 和 SpringBoot 以在我的日志输出中包含执行器返回的 git commit id? I want each line of output to include the git commit id of the boot app that generated the line of output.我希望每一行输出都包含生成输出行的启动应用程序的 git commit id。

By default, the git-commit-id-plugin creates a file named git.properties in the root of the classpath containing lots of properties, including git.commit.id .默认情况下, git-commit-id-plugin在类路径的根目录中创建一个名为git.properties的文件,其中包含许多属性,包括git.commit.id

You can import these properties in logback's config file, and access the properties anywhere within the logback configuration.您可以在 logback 的配置文件中 导入这些属性,并在 logback 配置中的任何位置访问这些属性。

For example, you can use it within a pattern like this:例如,您可以在这样的模式中使用它:

<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>

Or (since you tagged this question with logstash-logback-encoder ) you can include it in a JSON field via custom fields , or via the pattern provider like this:或者(因为您使用logstash-logback-encoder标记了这个问题)您可以通过自定义字段或通过模式提供程序将其包含在 JSON 字段中,如下所示:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM