<p>I am using Stackdriver Logging for Python and a Python logger at the same time. I am using the function <code>google.cloud.logging.logger.log_struct</code> (<a href="https://gcloud-python.readthedocs.io/en/latest/logging/logger.html" rel="nofollow noreferrer">https://gcloud-python.readthedocs.io/en/latest/logging/logger.html</a>) to log a JSON to the Stackdriver.</p>

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

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

<p>This is a snippet showing how I am doing the logging:</p>

<pre><code>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))
</code></pre>

<p>This is an example of what I get on the STDOUT on both the <code>VM instance</code> and the <code>Kubernetes Engine</code>:</p>

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

<p>This is what I see under the resource <code>Global</code> 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):
<a href="https://i.stack.imgur.com/yH452.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yH452.png" alt="enter image description here"></a></p>

<p>This is what I see under the resource <code>Google Kubernetes Engine</code>: The struct do not show, instead I see what is printed on <code>STDOUT</code>:
<a href="https://i.stack.imgur.com/CYNl7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CYNl7.png" alt="enter image description here"></a></p>

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

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

<p>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 <a href="https://cloud.google.com/logging/docs/agent/configuration#process-payload" rel="nofollow noreferrer">https://cloud.google.com/logging/docs/agent/configuration#process-payload</a> .</p>
