2

使用 GCP 日志记录客户端或 API 术语,如何嵌套日志条目,如图所示?

嵌套日志

4

1 回答 1

2

AppEngline 日志文档中解释了这个用例。

确保在traceId您发送的请求和应用程序日志中也将字段设置为非空值。这是Scala中的示例代码:

import com.google.cloud.MonitoredResource
import com.google.cloud.logging.Payload._
import com.google.cloud.logging._
import collection.JavaConverters._
import org.threeten.bp.Duration

val logging = LoggingOptions.getDefaultInstance().getService()
val traceId = "keasdfwxcbrbntpoiuwehrtiojsadf";

var firstEntry = {
  LogEntry.newBuilder(StringPayload.of("string-payload-one"))
    .setSeverity(Severity.ERROR)
    .setLogName("app")
    .setTimestamp(1519955138399L)
    .setResource(MonitoredResource.newBuilder("global").build())
    .setLabels(Map("environment" -> "testing").asJava)
    .setTrace(traceId)
    .build()
}

var midEntry = {
  LogEntry.newBuilder(StringPayload.of("string-payload-two"))
    .setSeverity(Severity.INFO)
    .setLogName("request")
    .setResource(MonitoredResource.newBuilder("global").build())
    .setHttpRequest(HttpRequest.newBuilder().setStatus(200).setRequestUrl("/about-us").setLatency(Duration.ofMillis(1234)).build())
    .setTimestamp(1519955137906L)
    .setLabels(Map("environment" -> "testing").asJava)
    .setTrace(traceId)
    .build()
}

var lastEntry = {
  LogEntry.newBuilder(StringPayload.of("string-payload-three"))
    .setSeverity(Severity.ERROR)
    .setLogName("app")
    .setResource(MonitoredResource.newBuilder("global").build())
    .setTimestamp(1519955138523L)
    .setLabels(Map("environment" -> "testing").asJava)
    .setTrace(traceId)
    .build()
}

logging.write(List(firstEntry, midEntry, lastEntry).asJava)

最后,日志条目应显示在其各自的日志中,并作为其请求的子项“交叉记录”,如下所示:

在此处输入图像描述

于 2018-02-28T00:06:57.943 回答