美文网首页
log4j2 日志动态记录git参数${git.commit.i

log4j2 日志动态记录git参数${git.commit.i

作者: 秋元_92a3 | 来源:发表于2020-09-08 18:16 被阅读0次

springboot项目日志记录版本git版本

1、在pom中引入对应的插件

<plugin>
                <groupId>pl.project13.maven</groupId>
                <artifactId>git-commit-id-plugin</artifactId>
                <version>2.2.5</version>
                <executions>
                    <execution>
                        <id>get-the-git-infos</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>revision</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
                    <prefix>git</prefix>
                    <verbose>false</verbose>
                    <dateFormat>yyyy-MM-dd HH:mm:ss</dateFormat>
                    <generateGitPropertiesFile>true</generateGitPropertiesFile>
                    <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties
                    </generateGitPropertiesFilename>
                    <format>properties</format>
                    <!-- git.commit.id.abbrev属性值的长度,取值范围在[2, 40],默认值7 -->
                    <abbrevLength>7</abbrevLength>
                    <gitDescribe>
                        <skip>false</skip>
                        <always>false</always>
                        <dirty>-dirty</dirty>
                    </gitDescribe>
                </configuration>
            </plugin>

注意,这里插件生成的git.properties文件的格式properties类型的,上一篇中对应纯java项目完成该功能的配置,这里的格式json格式的。

2、log4j配置文件中引入git占位符

appender中添加向kafka中输入的日志模板,在模板中,包含git.commit.id.abbrev占位符:

<Kafka name="kafka" topic="log-collect" ignoreExceptions="false">
            <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
            <LogstashLayout dateTimeFormatPattern="yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"
                            eventTemplateUri="classpath:LogstashJsonEventLayoutV1.json"
                            prettyPrintEnabled="true"
                            stackTraceEnabled="true"
                            locationInfoEnabled="true">
                <EventTemplateAdditionalFields>
                    <KeyValuePair key="appName" value="log-git-springboot-demo"/>
                    <KeyValuePair key="env" value="beta"/>
                    <KeyValuePair key="git" value="${git.commit.id.abbrev}"/>
                    <KeyValuePair key="tag" value="${git.tags}"/>
                </EventTemplateAdditionalFields>
            </LogstashLayout>
            <Property name="bootstrap.servers">${kafka.servers}</Property>
            <Property name="request.timeout.ms">5000</Property>
            <Property name="transaction.timeout.ms">5000</Property>
            <Property name="max.block.ms">3000</Property>
        </Kafka>

属性中增加如下两个配置:占位符内部冒号前面的git,指的是插件,在下一点中会处理这个插件

 <property name="git.commit.id.abbrev">${git:git.commit.id.abbrev}</property>
        <property name="git.tags">${git:git.tags}</property>

3、自定义git插件

新增类GitLookup,作为git插件,完成log中git占位符的替换,内容如下:

package com.log.git.springboot;

import cn.hutool.setting.dialect.Props;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.lookup.AbstractLookup;
import org.apache.logging.log4j.core.lookup.StrLookup;

/**
 * GitLookup
 *
 * @author drebander
 * @since 2020-09-09 11:31 上午
 **/
@Plugin(name = "git", category = StrLookup.CATEGORY)
public class GitLookup extends AbstractLookup {
    private static final String PROPERTIES_NAME = "git.properties";

    private static final Props PROPS = new Props(PROPERTIES_NAME);

    @Override
    public String lookup(final LogEvent event, final String key) {
        return PROPS.getStr(key);
    }
}

4、验证效果

完成上面的操作之后,就会发现在输出的日志中,对应的git属性已经替换成了对应的值了。上面的源码中涉及到了hutools的使用,maven对于hutools的引用参照下面:

 <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.3.0</version>
        </dependency>

相关文章

网友评论

      本文标题:log4j2 日志动态记录git参数${git.commit.i

      本文链接:https://www.haomeiwen.com/subject/spfyektx.html