从API在Cloud Logging Viewer中创建嵌套日志条目?

时间:2015-07-14 02:58:41

标签: google-cloud-logging

我想将日志从运行在计算引擎上的自定义应用发布到云日志API。

但是,我想获得嵌套日志(如AppEngine日志)。这可能通过api吗?

2 个答案:

答案 0 :(得分:1)

此用例在AppEngline Logs docs中解释。

确保还在要发送的请求和应用日志上将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.DEBUG)
    .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.DEBUG)
    .setLogName("app")
    .setResource(MonitoredResource.newBuilder("global").build())
    .setTimestamp(1519955138523L)
    .setLabels(Map("environment" -> "testing").asJava)
    .setTrace(traceId)
    .build()
}

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

最后,日志条目应显示在各自的日志中,并且"交叉记录"作为他们要求的孩子,像这样:

enter image description here

答案 1 :(得分:-1)

3年后,我们在这里......

来自app引擎的概念是通过长时间运行来分组

您只需要在api调用中使用对象operation指定参数LogentryOperation

以下是此对象的参考资料

https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogEntryOperation