本文介绍了如何在 App Engine Flex Java 环境中使用 Stackdriver Structured Logging的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google App Engine 柔性环境自动将 stdout 和 stderr 传送到 Stackdriver (Google Cloud Logging).但这仅支持没有任何元数据的纯文本日志消息(甚至不支持日志级别).

Google App Engine flexible environment automatically pipes stdout and stderr to Stackdriver (Google Cloud Logging). But this only supports plain text log message without any metadata (not even logging levels).

我找到了用于谷歌云日志记录的 Logback appender.

I found Logback appender for google cloud logging.

https://cloud.google.com/logging/docs/setup/java

但这似乎还不支持结构化日志记录.而且,它为引擎盖下的每个日志条目进行 GRPC 调用.所以,我想知道它的可扩展性如何(尤其是与当前具有单独 Fluentd 代理处理日志的应用引擎结构相比).

But this does not seem to support structured logging yet.And also, it makes GRPC calls for every log entry under the hood. So, I wonder how scalable it is (especially compare to current app engine structure which has separate Fluentd agent handles logs).

是否有任何现成的或简单的解决方案可以将结构化日志从 App Engine 发送到 Stackdriver?

Is there any out of box or simple solution to send structured log from App Engine to Stackdriver?

推荐答案

你可以使用 Logback appender +Implement LoggingEnhacer

You could use the Logback appender + Implement LoggingEnhacer

public class LogEnhancer implements LoggingEnhancer {


  @Override
  public void enhanceLogEntry(LogEntry.Builder logEntry) {
    // add Labels
    logEntry.addLabel("project", "conversational-services");

    // Transform textPayload to JSONPayload
    ObjectMapper mapper = new ObjectMapper();
    Builder structBuilder = Struct.newBuilder();
    String textPayload = logEntry.build().getPayload().getData().toString();
    try {
      // Validate JSON Payload
      mapper.readTree(textPayload);
      JsonFormat.parser().merge(textPayload, structBuilder);
      logEntry.setPayload(JsonPayload.of(structBuilder.build()));
    } catch (InvalidProtocolBufferException e) {
      // Error reporting an error! FML
      System.err.println(e.getMessage());
    } catch (IOException e) {
      // Do nothing (there is not a JSON Payload)
    }

  }

}

这个类在 JSONPayload 中添加标签和转换 JSON 字符串

This class add labels and transform a JSON String in a JSONPayload

您需要在 logback.xml 文件中指定 LoggingEnhacer

You need to specify the LoggingEnhacer on the logback.xml file

<!DOCTYPE configuration>
<configuration>
  <appender name="CLOUD" class="com.google.cloud.logging.logback.LoggingAppender">

    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
      <level>INFO</level>
    </filter>
    <log>application.log</log> 
    <resourceType>gae_app</resourceType> 
    <!--  References to the LoggingEnhacer class -->
     <enhancer>[path_for_your_logging_enhancer_class]</enhancer>
    <flushLevel>WARN</flushLevel> 
  </appender>

  <root level="info">
    <appender-ref ref="CLOUD" />
  </root>
</configuration>

这篇关于如何在 App Engine Flex Java 环境中使用 Stackdriver Structured Logging的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 09:38