title: Gunicorn log errors in Google Cloud Platform
body: <p>I am running a Django REST API on Google Kubernetes Engine, using Gunicorn for my WSGI server. When my application encounters a 500 server error, the Python stack trace is not showing up in the <a href="https://console.cloud.google.com/logs/viewer" rel="nofollow noreferrer">GCP Logging console</a> (in the "GKE Container" resource). In a different Django project of mine which uses Daphne for the ASGI/WSGI server, this traceback <em>is</em> being properly logged. Even more strange, the Gunicorn application has properly logged errors before, just a few weeks ago. Those errors appear in the <a href="https://console.cloud.google.com/errors" rel="nofollow noreferrer">Error Reporting console</a> as well.</p>
  
  <p>To be clear, this is the type of information that I would like to see in the GCP logs:</p>
  
  <pre><code>Internal Server Error: /v1/user/errant-endpoint
  Traceback (most recent call last):
    File "/path/to/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
      response = get_response(request)
  
    ...
  
    File "/path/to/project/file.py", line 176, in my_file
      print(test)
  NameError: name 'test' is not defined
  </code></pre>
  
  <p>For the Gunicorn project, some of the Python tracebacks are logged, like this one when Gunicorn is started:</p>
  
  <pre><code>/usr/local/lib/python3.6/site-packages/django/db/models/fields/__init__.py:1421: RuntimeWarning: DateTimeField User.last_login received a naive datetime (2019-02-04 05:49:47.530648) while time zone support is active.
  </code></pre>
  
  <p>With 500 errors, however, only the HTTP info is logged:</p>
  
  <pre><code>[04/Feb/2019:06:03:58 +0000] "POST /v1/errant-endpoint HTTP/1.1" 500
  </code></pre>
  
  <p>I've looked up the resources for setting up <a href="https://cloud.google.com/logging/docs/setup/python" rel="nofollow noreferrer">Stackdriver Logging</a> and <a href="https://cloud.google.com/error-reporting/docs/setup/python" rel="nofollow noreferrer">Stackdriver Error Reporting</a>, but neither of these seem to apply because 1) that only appears to work when you want to explicitly log your errors (as in <code>client.report_exception()</code>), and 2) Error Reporting has caught previous errors without my setting up, so it seems to be possible without having to install those client libraries.</p>
  
  <p>There are so many variables at play here that I'm not sure where to start. I may not have provided enough information here in order to properly diagnose this (docker setup, kubernetes configuration, etc.), but I figured I may be misunderstanding something fundamental about this process, and that someone could be so kind as to enlighten me.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I found in the <a href="https://cloud.google.com/kubernetes-engine/docs/how-to/logging#enabling_stackdriver_logging" rel="nofollow noreferrer">GKE documentation</a> how to ensure that the Stackdriver logging is enabled for my cluster. Still no luck.</p>
  
  <p><strong>UPDATE (partial solution):</strong></p>
  
  <p>I set <code>DEBUG = True</code> in <code>settings.py</code>, which prompted the errors to start being logged in Stackdriver Logging and Error Reporting. I've gone ahead and done this for my canary environment, but it's less than ideal, since it exposes some of my backend code. Still not sure why this works for my other GCP project without running in debug mode.</p>