Google Stackdriver not showing struct entry log as expected from Google Kubernetes Engine

10/26/2018

I am using Stackdriver Logging for Python and a Python logger at the same time. I am using the function google.cloud.logging.logger.log_struct (https://gcloud-python.readthedocs.io/en/latest/logging/logger.html) to log a JSON to the Stackdriver.

I am able to view the logs as expected in the log viewer with the selected resource Global when I am running my script using a Google Compute Engine VM instance. The struct I am logging is recorded properly in jsonPayload.

However, when the logging is coming from a Google Kubernetes Engine, the logger view does not show the structs I passed, but rather what is printed on stdout. How do I make sure I observe the same behaviour from the Google Compute Engine VM instance and a Google Kubernetes Engine?

This is a snippet showing how I am doing the logging:

import google.cloud.logging
import logging

logging_client = google.cloud.logging.Client()
# connects the logger to the root logging handler
cloud_logger = logging_client.logger('email_adaptor')


formatter = logging.Formatter(
            '%(asctime)s - %(levelname)s - %(message)s - %(lineno)d - %(filename)s')
# get the logger a name
logger = logging.getLogger('email_adaptor')
# set a level for the logger
logger.setLevel(logging.DEBUG)

# make the logger write on stdout
stdout_alarm_log = logging.StreamHandler(sys.stdout)
stdout_alarm_log.setFormatter(formatter)
logger.addHandler(stdout_alarm_log)


struct = {'message':'Processed Alarm', 'valid': True}
cloud_logger.log_struct(struct, severity='INFO')

logger.info(str(struct))

This is an example of what I get on the STDOUT on both the VM instance and the Kubernetes Engine:

2018-10-26 12:30:20,008 - INFO - Processed Alarm {'valid': True} - 79 - log.py
INFO:email_adaptor:Processed Alarm {'valid': True}

This is what I see under the resource Global in the Google Log Viewer (the logs are ones from my VM instance, and do not correspond to the example I gave in the snippet code): enter image description here

This is what I see under the resource Google Kubernetes Engine: The struct do not show, instead I see what is printed on STDOUT: enter image description here

-- christegho
google-cloud-stackdriver
kubernetes
logging
python
stackdriver

1 Answer

10/29/2018

The Stackdriver library calls in the code write against “global” and the structured log entries from your GKE containers will show under “Global” resource.

The “GKE Container” resource will show logging written to stdout and stderr, which are ingested by default.

To write structured logs to stdout/stderr and access them in Stackdriver, the only structured format that the logging agent will accept is JSON. You must configure your logger to output JSON for it to be picked up as a structured log entry. More information at https://cloud.google.com/logging/docs/agent/configuration#process-payload .

-- dopplesoldner
Source: StackOverflow