Search code examples
pythondjangogoogle-app-engineloggingcolors

How does one make logging color in Django/Google App Engine?


If one iswriting a Django/ Google App Engine application and would like to have logs that are conveniently conspicuous based on color (i.e. errors in red), how does one set that up?

I've copied the helpful solution from this question, but I'm not sure how to integrate it into Django/Google App Engine.

I figured one would put the following in the application's main.py (i.e. essentially from the example here: Running Django on Google App Engine):

from contrib.utils import ColouredLogger # from the SO question above
logging.setLoggerClass(ColouredLogger)

... where contrib.utils is where I put airmind's code from the above link to his SO answer.

However, that doesn't seem to do anything to the output to the console for GAE, which continues to be in the original format + plain color.


Solution

  • We use colorlog and it does exactly what you expect.

    For posterity, the formatter config we use is:

    'color': {
        '()': 'colorlog.ColoredFormatter',
        'format': '%(log_color)s%(levelname)-8s %(message)s',
        'log_colors': {
            'DEBUG':    'bold_black',
            'INFO':     'white',
            'WARNING':  'yellow',
            'ERROR':    'red',
            'CRITICAL': 'bold_red',
        },
    }