How to log to Stackdriver from GKE in a Java application

8/5/2019

I'm running a Java/Scala application in a container on GKE, and the logs are not populating in Stackdriver.

The GCP Console is showing that "Stackdriver Kubernetes Engine Monitoring" is enabled on the cluster, and "Legacy Stackdriver Logging" is disabled.

I've tried two types of logback files, one using ConsoleAppender and one using com.google.cloud.logging.logback.LoggingAppender, but neither works.

If I run a node.js app in GKE, then the console.log entries do show in Stackdriver.

To add some more detail that may help clarify, my main application container is a Node app that spawns a Java app. The Node app logs show in Stackdriver, but the Java app logs do not. I do set the GOOGLE_APPLICATION_CREDENTIALS environment variable to a service account I'm using with permission to write to Datastore, Cloud Storage and PubSub. I didn't adding logging permissions to that account, because I figured there is another container or lower level process that writes the logs.

The last logback file I tried is:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

   <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
       <target>System.out</target>
       <encoder>
           <pattern>%X{akkaTimestamp} %-5level[%thread] %logger{0} - %msg%n</pattern>
       </encoder>
    </appender>

    <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
       <appender-ref ref="STDOUT"/>
    </appender>

    <logger name="akka" level="INFO" />

    <root level="INFO">
       <appender-ref ref="STDOUT"/>
    </root>

</configuration>

What am I missing?

-- jacob
google-cloud-logging
google-cloud-stackdriver
google-kubernetes-engine
stackdriver

2 Answers

8/6/2019

Currently this is on Beta and is in a pre-release state and might change or have limited support.

But what you are looking is here:

-- Alan Damian Rodriguez
Source: StackOverflow

8/7/2019

I figured out the problem. When the node app spawns the Java process, it needs to configure the Java process to inherit the parent's stdio, like this:

const child = spawn(cmd, args, { stdio: 'inherit' ))
-- jacob
Source: StackOverflow