[Fixed]-Output colored logs from django through supervisor and docker-compose

10👍

Here is a minimal working example for Django application served with Gunicorn. The trick is to make sure Gunicorn loggers are configured to use a handler that utilizes a colored formatter.

settings.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'colored_verbose': {
            '()': 'colorlog.ColoredFormatter',
            'format': "%(log_color)s%(levelname)-8s%(red)s%(module)-30s%(reset)s %(blue)s%(message)s"
        },
    },
    'handlers': {
        'colored_console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'colored_verbose'
        }
    },
    'loggers': {
        '': {
            'level': 'INFO',
            'handlers': ['colored_console'],
        },
        'gunicorn.access': {
            'handlers': ['colored_console']
        },
        'gunicorn.error': {
            'handlers': ['colored_console']
        }
    }
}

Terminal output

Colored logs

Bonus

The best way (from my experience) to check your logging configuration is to use logging_tree.

Simply execute this in your application context (in Django shell for example)

import logging_tree
logging_tree.printout()

and it should print a nice representation of the existing loggers.

I went a step further and added this to my urls.py so I could see the logging configuration after all the loggers are installed.

from django.conf.urls import url
from django.http import HttpResponse
import logging_tree

urlpatterns = [
    url(r'^loggers', loggers),
]

def loggers(request):
    """
    Returns a representation of the existing loggers
    """
    return HttpResponse(logging_tree.format.build_description()[:-1])

It should return something similar to this

<--""
   Level INFO
   Handler Stream <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
     Level INFO
     Formatter <colorlog.colorlog.ColoredFormatter object at 0x1103a1668>
   |
   o<--"django"
   |   Level INFO
   |   Handler Stream <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
   |     Level INFO
   |     Filter <django.utils.log.RequireDebugTrue object at 0x11047d518>
   |   Handler <AdminEmailHandler (ERROR)>
   |     Level ERROR
   |     Filter <django.utils.log.RequireDebugFalse object at 0x11053a7f0>
   |   |
   |   o<--"django.db"
   |   |   Level NOTSET so inherits level INFO
   |   |   |
   |   |   o<--"django.db.backends"
   |   |       Level NOTSET so inherits level INFO
   |   |       |
   |   |       o<--"django.db.backends.schema"
   |   |           Level NOTSET so inherits level INFO
   |   |
   |   o<--"django.request"
   |   |   Level NOTSET so inherits level INFO
   |   |
   |   o   "django.server"
   |   |   Level INFO
   |   |   Propagate OFF
   |   |   Handler Stream <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
   |   |     Level INFO
   |   |     Formatter <django.utils.log.ServerFormatter object at 0x11053a630>
   |   |
   |   o<--"django.template"
   |       Level NOTSET so inherits level INFO
   |
   o<--"gunicorn"
       Level NOTSET so inherits level INFO
       |
       o   "gunicorn.access"
       |   Level INFO
       |   Propagate OFF
       |   Handler Stream <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
       |     Level INFO
       |     Formatter <colorlog.colorlog.ColoredFormatter object at 0x1103a1668>
       |
       o   "gunicorn.error"
       |   Level INFO
       |   Propagate OFF
       |   Handler Stream <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
       |     Level INFO
       |     Formatter <colorlog.colorlog.ColoredFormatter object at 0x1103a1668>
       |
       o<--"gunicorn.http"
          Level NOTSET so inherits level INFO
           |
           o<--"gunicorn.http.wsgi"
               Level NOTSET so inherits level INFO

Leave a comment