gpt4 book ai didi

google-cloud-logging - 从 API 在 Cloud Logging Viewer 中创建嵌套的日志条目?

转载 作者:行者123 更新时间:2023-12-02 03:23:56 24 4
gpt4 key购买 nike

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

但是,我想要获取嵌套日志(如 AppEngine 日志行)。这可以通过 api 实现吗?

最佳答案

此用例在 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

关于google-cloud-logging - 从 API 在 Cloud Logging Viewer 中创建嵌套的日志条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31396882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com