使用用于 spring boot 应用程序的 json 附加程序进行 GCP 日志记录

发布于 2025-01-13 11:48:52 字数 3223 浏览 0 评论 0原文

我正在尝试添加 gcp 跟踪并记录到 Spring Boot 应用程序,但出现此错误


应用程序无法启动


描述: com.google.cloud.spring.logging.LoggingWebMvcConfigurer 中构造函数的参数 1 需要类型为 com.google.cloud.spring.core.GcpProjectIdProvider 的 bean,但无法找到。 行动: 考虑在配置中定义 com.google.cloud.spring.core.GcpProjectIdProvider 类型的 bean。

在 gradle.build 中,我添加了依赖项

implementation 'org.springframework.cloud:spring-cloud-gcp-autoconfigure:1.1.0.RELEASE'
implementation 'com.google.cloud:spring-cloud-gcp-starter-logging:2.0.0'
implementation 'org.springframework.cloud:spring-cloud-gcp-starter-trace:1.2.5.RELEASE'

,并在 logback-spring.xml 中的 application.properties 中

spring.main.allow-bean-definition-overriding=true
spring.cloud.gcp.logging.enabled=true
spring.sleuth.sampler.probability=1.0

添加了此配置,

<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />

    <springProfile name="dev |tcm | ver | prd">
        <!--include resource="com/google/cloud/spring/logging/logback-json-appender.xml"/-->
        <property name="projectId" value="${projectId:-${GOOGLE_CLOUD_PROJECT}}"/>
        <appender name="CONSOLE_JSON" class="ch.qos.logback.core.ConsoleAppender">
            <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
                <layout class="com.google.cloud.spring.logging.StackdriverJsonLayout">
                    <!--projectId>${projectId}</projectId-->

                    <!--<includeTraceId>true</includeTraceId>-->
                    <!--<includeSpanId>true</includeSpanId>-->
                    <!--<includeLevel>true</includeLevel>-->
                    <!--<includeThreadName>true</includeThreadName>-->
                    <!--<includeMDC>true</includeMDC>-->
                    <!--<includeLoggerName>true</includeLoggerName>-->
                    <!--<includeFormattedMessage>true</includeFormattedMessage>-->
                    <!--<includeExceptionInMessage>true</includeExceptionInMessage>-->
                    <!--<includeContextName>true</includeContextName>-->
                    <!--<includeMessage>false</includeMessage>-->
                    <!--<includeException>false</includeException>-->
                    <!--<serviceContext>
                          <service>service-name</service>
                          <version>service-version</version>
                        </serviceContext>-->
                    <!--<customJson>{"custom-key": "custom-value"}</customJson>-->
                </layout>
            </encoder>
        </appender>
        <root level="INFO">
            <appender-ref ref="CONSOLE_JSON"/>
        </root>
    </springProfile>
</configuration>

有人遇到类似问题并设法修复它吗?

I am trying adding gcp trace and logging to spring boot app but I get this error


APPLICATION FAILED TO START


Description:
Parameter 1 of constructor in com.google.cloud.spring.logging.LoggingWebMvcConfigurer required a bean of type com.google.cloud.spring.core.GcpProjectIdProvider that could not be found.
Action:
Consider defining a bean of type com.google.cloud.spring.core.GcpProjectIdProvider in your configuration.

in gradle.build, I added the dependencies

implementation 'org.springframework.cloud:spring-cloud-gcp-autoconfigure:1.1.0.RELEASE'
implementation 'com.google.cloud:spring-cloud-gcp-starter-logging:2.0.0'
implementation 'org.springframework.cloud:spring-cloud-gcp-starter-trace:1.2.5.RELEASE'

and in application.properties

spring.main.allow-bean-definition-overriding=true
spring.cloud.gcp.logging.enabled=true
spring.sleuth.sampler.probability=1.0

in logback-spring.xml I have this configurations

<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />

    <springProfile name="dev |tcm | ver | prd">
        <!--include resource="com/google/cloud/spring/logging/logback-json-appender.xml"/-->
        <property name="projectId" value="${projectId:-${GOOGLE_CLOUD_PROJECT}}"/>
        <appender name="CONSOLE_JSON" class="ch.qos.logback.core.ConsoleAppender">
            <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
                <layout class="com.google.cloud.spring.logging.StackdriverJsonLayout">
                    <!--projectId>${projectId}</projectId-->

                    <!--<includeTraceId>true</includeTraceId>-->
                    <!--<includeSpanId>true</includeSpanId>-->
                    <!--<includeLevel>true</includeLevel>-->
                    <!--<includeThreadName>true</includeThreadName>-->
                    <!--<includeMDC>true</includeMDC>-->
                    <!--<includeLoggerName>true</includeLoggerName>-->
                    <!--<includeFormattedMessage>true</includeFormattedMessage>-->
                    <!--<includeExceptionInMessage>true</includeExceptionInMessage>-->
                    <!--<includeContextName>true</includeContextName>-->
                    <!--<includeMessage>false</includeMessage>-->
                    <!--<includeException>false</includeException>-->
                    <!--<serviceContext>
                          <service>service-name</service>
                          <version>service-version</version>
                        </serviceContext>-->
                    <!--<customJson>{"custom-key": "custom-value"}</customJson>-->
                </layout>
            </encoder>
        </appender>
        <root level="INFO">
            <appender-ref ref="CONSOLE_JSON"/>
        </root>
    </springProfile>
</configuration>

anyone had similar issue and managed to fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请远离我 2025-01-20 11:48:52

这些依赖项的版本不一致:com.google.cloud:spring-cloud-gcp-starter-logging:2.0.0org.springframework.cloud:spring-cloud-gcp -autoconfigure:1.1.0.RELEASE 和 org.springframework.cloud:spring-cloud-gcp-starter-trace:1.2.5.RELEASE。

对所有三个依赖项使用相同的版本 - 最新的是 3.1.0。另请注意,在 1.x 和 2.x+ 之间,组和包名称更改为 com.google.cloud

These dependencies' versions are not mutually consistent: com.google.cloud:spring-cloud-gcp-starter-logging:2.0.0, org.springframework.cloud:spring-cloud-gcp-autoconfigure:1.1.0.RELEASE and org.springframework.cloud:spring-cloud-gcp-starter-trace:1.2.5.RELEASE.

Use the same version for all three dependencies -- the latest is 3.1.0. Also notice that between 1.x and 2.x+, the group and package names changed to com.google.cloud.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文