簡體   English   中英

在 GCP Cloud Logging Spring Boot 中包含 ApplicationName

[英]Include ApplicationName in GCP Cloud Logging Spring Boot

在基於 spring 啟動的微服務架構中,我將 prod 中的所有日志發送到 GCP 日志記錄,這工作正常。 但日志不包含應用程序名稱。 由於微服務使用的是通用的starter artifcat,所以類似的日志很多,很難判斷是哪個服務產生的日志。 那么如何配置日志記錄以包含應用程序名稱呢? 基本上,我正在尋找一種方法來確定日志來自哪個微服務?

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>

下面是 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>

通過創建我自己的 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM