繁体   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