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?
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:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With