简体   繁体   English

在 GCP Cloud Logging Spring Boot 中包含 ApplicationName

[英]Include ApplicationName in GCP Cloud Logging Spring Boot

In microservices arch, built on spring boot, i am sending all logs in prod to GCP logging, which is working fine.在基于 spring 启动的微服务架构中,我将 prod 中的所有日志发送到 GCP 日志记录,这工作正常。 But the logs doesn't include the application name.但日志不包含应用程序名称。 Since, the microservices uses a common starter artifcat, so there are a lot of similar logs, and it is hard to identify that which services is producing the log.由于微服务使用的是通用的starter artifcat,所以类似的日志很多,很难判断是哪个服务产生的日志。 So how to configure the logging to also include application name?那么如何配置日志记录以包含应用程序名称呢? Basically, i am looking for a way to figure that the log is coming from which microservice?基本上,我正在寻找一种方法来确定日志来自哪个微服务?

Changes in pom.xml pom.xml 的变化
<properties>
        <spring-cloud-gcp-starter-logging.version>1.2.8.RELEASE</spring-cloud-gcp-starter-logging.version>
</properties>

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-gcp-starter-logging</artifactId>
   <version>${spring-cloud-gcp-starter-logging.version}</version>
</dependency>

And below is logback-spring.xml下面是 logback-spring.xml

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

    <springProfile name="dev">
        <root level="INFO">
            <appender-ref ref="CONSOLE"/>
            <!--            <appender-ref ref="CONSOLE_JSON"/>-->
        </root>
    </springProfile>

    <springProfile name="prod">
        <root level="INFO">
            <appender-ref ref="CONSOLE"/>
            <!--            <appender-ref ref="CONSOLE_JSON"/>-->
            <appender-ref ref="STACKDRIVER"/>
        </root>
    </springProfile>

</configuration>

Solved it by creating my own appender通过创建我自己的 appender 来解决它

<appender name="MY_STACKDRIVER" class="org.springframework.cloud.gcp.logging.LoggingAppender">
        <log>${STACKDRIVER_LOG_NAME}</log> <!-- Optional : default java.log -->
        <enhancer>com.xyz.logging.MyLoggingEnhancer</enhancer>
        <flushLevel>${STACKDRIVER_LOG_FLUSH_LEVEL}</flushLevel> <!-- Optional : default ERROR -->
    </appender>


@Component
public class MyLoggingEnhancer extends TraceIdLoggingEnhancer {

    private static final String MICROSERVICE_NAME = "serviceName";

    @Override
    public void enhanceLogEntry(LogEntry.Builder builder) {
        super.enhanceLogEntry(builder);
        if (StaticBeanUtil.getInstance() != null) {
            builder.addLabel(MICROSERVICE_NAME, StaticBeanUtil.getInstance().getApplicationName());
        }
    }

}

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

相关问题 可以使用 gcp 凭据登录 spring 启动应用程序 - Can use gcp credentials for logging in spring boot application 如何将 spring 启动应用程序部署到 GCP 云功能? - How to deploy spring boot application to GCP cloud functions? Cloud Run GCP Spring 启动无法读取输入文件 pdf - Cloud Run GCP Spring Boot cant read input file pdf 强调 spring 引导服务时消耗的 GCP Postgres 连接数(使用 SQL 云代理) - GCP Postgres connections consumed when stressing spring boot service (using SQL Cloud proxy) 在 spring 启动 2.6.1 中使用 spring-cloud-gcp-pub-sub-stream-binder 时未发现内部方法错误 - internal method not found error while using spring-cloud-gcp-pub-sub-stream-binder in spring boot 2.6.1 无法将 spring 引导指标发布到 GCP 堆栈驱动程序 - Unable to publish spring boot metrics to GCP stackdriver Spring 启动 GCP PUBSUB 消费者应用程序 - Spring boot GCP PUBSUB Consumer application 使用 Spring Cloud GCP 的精确一次交付语义 - Exactly once delivery semantics with Spring Cloud GCP 如何在云日志中获取 GCP 区域故障事件/活动日志 - How to get the GCP zonal failure event / activity logs in cloud logging GCP Cloud Logging 花费那么多成本,如何禁用它? - GCP Cloud Logging take so much cost, and how to disable it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM