I cannot get logback to reference a generated git.properties file after I've built and deployed my service, what am I doing wrong?

huangapple go评论82阅读模式
英文:

I cannot get logback to reference a generated git.properties file after I've built and deployed my service, what am I doing wrong?

问题

我在Maven中有以下的插件设置:

<plugins>
    <!-- This will add a git.properties file in the resources folder with commit information we can reference-->
    <plugin>
        <groupId>io.github.git-commit-id</groupId>
        <artifactId>git-commit-id-maven-plugin</artifactId>
        <version>6.0.0</version>
        <executions>
            <execution>
                <id>get-the-git-infos</id>
                <goals>
                    <goal>revision</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <generateGitPropertiesFile>true</generateGitPropertiesFile>
            <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
        </configuration>
    </plugin>
</plugins>

我的 logback-spring.xml 如下所示:

<configuration>
    <springProperty scope="context" name="applicationVersion" source="project.version" defaultValue="unknown" />
    <springProfile name="!dev">
        <springProperty scope="context" name="gitCommitId" source="git.commit.id.abbrev" defaultValue="unknown" />
    </springProfile>

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LogstashEncoder">
        </encoder>
    </appender>

    <root level="INFO">
        <appender-ref ref="CONSOLE" />
    </root>
</configuration>

applicationVersion 正如预期地出现在日志中,但这是从 application.properties 文件中引用的。
git.properties 文件生成并放置在以下位置:

  • target/my-service-7.0-SNAPSHOT/WEB-INF/classes/git.properties
  • target/classes/git.properties

如预期,当我运行开发配置时,gitCommitId 不会出现在日志中,但当我部署它时,它会出现在日志中,其默认值为 "unknown"。

我做错了什么?

英文:

I have the following plugin setup in maven:

   &lt;plugins&gt;
            &lt;!-- This will add a git.properties file in the resources folder with commit information we can reference--&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;io.github.git-commit-id&lt;/groupId&gt;
                &lt;artifactId&gt;git-commit-id-maven-plugin&lt;/artifactId&gt;
                &lt;version&gt;6.0.0&lt;/version&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;id&gt;get-the-git-infos&lt;/id&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;revision&lt;/goal&gt;
                        &lt;/goals&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
                &lt;configuration&gt;
                    &lt;generateGitPropertiesFile&gt;true&lt;/generateGitPropertiesFile&gt;
                    &lt;generateGitPropertiesFilename&gt;${project.build.outputDirectory}/git.properties&lt;/generateGitPropertiesFilename&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;

My logback-spring.xml looks like this:

&lt;configuration&gt;
    &lt;springProperty scope=&quot;context&quot; name=&quot;applicationVersion&quot; source=&quot;project.version&quot; defaultValue=&quot;unknown&quot; /&gt;
    &lt;springProfile name=&quot;!dev&quot;&gt;
        &lt;springProperty scope=&quot;context&quot; name=&quot;gitCommitId&quot; source=&quot;git.commit.id.abbrev&quot; defaultValue=&quot;unknown&quot; /&gt;
    &lt;/springProfile&gt;

    &lt;appender name=&quot;CONSOLE&quot; class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;
        &lt;encoder class=&quot;net.logstash.logback.encoder.LogstashEncoder&quot;&gt;
        &lt;/encoder&gt;
    &lt;/appender&gt;

    &lt;root level=&quot;INFO&quot;&gt;
        &lt;appender-ref ref=&quot;CONSOLE&quot; /&gt;
    &lt;/root&gt;
&lt;/configuration&gt;

The applicationVersion appears in the logs as expected, but this is referenced from the application.properties file.
The git.properties file is generated and placed in the following locations:

target/my-service-7.0-SNAPSHOT/WEB-INF/classes/git.properties

target/classes/git.properties

As expected, gitCommitId doesn't appear when I'm running the dev profile, but when I deploy it, it appears in the logs with the default value "unknown".

What am I doing wrong?

答案1

得分: 0

我改变了这个策略,不再尝试直接访问 git.properties,而是将其导入到 application.properties,然后像其他属性一样,可以被 logback 访问。
解决方案请参见此处:https://stackoverflow.com/a/76686438/571875

英文:

I changed my strategy for this, instead of trying to access git.properties directly, I now import it into application.properties instead, then it is accessible to logback as the other properties are.
See here for the solution: https://stackoverflow.com/a/76686438/571875

huangapple
  • 本文由 发表于 2023年7月10日 16:21:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定