quano
quano

Reputation: 19152

How to enable logging for deployed Django app running on GAE (Google App Engine)?

When using DEBUG = False for settings.py, Django will not output any errors even when severe (Http status 500) errors occur.

I've tried to add a LOGGING entry for my settings.py, but it seems like when it is deployed on GAE, the server fails instantly upon every request.

How do you enable error logging (with traceback) for a deployed django app running on GAE?

Upvotes: 2

Views: 231

Answers (1)

quano
quano

Reputation: 19152

You can create your own so called "middleware", which is a system in Django for hooking into the request/response processing.

By defining a "process_exception" method, you can catch any exception that a view raises, and do with it what you like.

Here's an example (<YOUR-APP>/middleware.py):

import sys
import traceback

class HandleExceptionsMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)

        return response

    def process_exception(self, request, exception):
        print("Internal Server Error: " + request.get_full_path(), file=sys.stderr)
        print(traceback.format_exc(), file=sys.stderr)

Then, in settings.py, you can add your middleware:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
    '<YOUR-APP>.middleware.HandleExceptionsMiddleware',
]

In this case, we're simply writing to stderr which will make it visible in GAE.

Upvotes: 1

Related Questions