[英]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?
基本上,我正在寻找一种方法来确定日志来自哪个微服务?
<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.