Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating nested log entries in Cloud Logging Viewer from API?

I'd like to publish logs from a custom app running on a compute engine to the cloud logging API.

However, I'd like to get nested logs (like AppEngine loglines). Is this possible through the api?

like image 746
aloo Avatar asked Mar 13 '26 20:03

aloo


1 Answers

This use-case is explained in the AppEngline Logs docs.

Make sure to also set the traceId fields to non-null values on the request and app logs which you are sending. Here's sample code in 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)

At the end, the log entries should show up both in their individual logs and "cross-logged" as children of their requests, like this:

enter image description here

like image 86
gsquaredxc Avatar answered Mar 16 '26 20:03

gsquaredxc