简体   繁体   中英

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. 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. 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
<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

<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 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());
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM