Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Suggestions and input would be much appreciated.

Cheers, Brian

like image 828
Brian M. Hunt Avatar asked Aug 16 '09 21:08

Brian M. Hunt


3 Answers

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',
    },
}
like image 68
Alec Thomas Avatar answered Nov 09 '22 18:11

Alec Thomas


Django already has support for color output through the 'DJANGO_COLORS' environment variable used for example when running the built in development server. Some person has noticed this and created a plug-and-play solution https://github.com/tiliv/django-colors-formatter; with that package on the project's python path my logging settings.py is as follow:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'formatters': {
        'verbose': {
            '()': 'djangocolors_formatter.DjangoColorsFormatter', # colored output
            'format': '%(levelname)s %(name)s %(asctime)s %(module)s %(process)d %(thread)d %(pathname)s@%(lineno)s: %(message)s'
        },
        'simple': {
            '()': 'djangocolors_formatter.DjangoColorsFormatter', # colored output
            'format': '%(levelname)s %(name)s %(filename)s@%(lineno)s: %(message)s'
        },
    },
     # omitting the handler 'level' setting so that all messages are passed and we do level filtering in 'loggers'
    'handlers': {
        'null': {
            'class':'django.utils.log.NullHandler',
        },
        'console':{
            'class':'logging.StreamHandler',
            'formatter': 'simple',
        },
        'mail_admins': {
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler',
            'formatter': 'verbose'
        }
    },
    'loggers': {
        '': { 
            'handlers': ['mail_admins', 'console'],
            'level': 'WARNING',
        },
    }
}

Sample console logging output using django-colors-formatter: Sample console logging output

like image 28
Daniel Sokolowski Avatar answered Nov 09 '22 16:11

Daniel Sokolowski


I also wanted color output for the dev_appserver. I found the solutions here a little OTT (all I wanted was to make my logging.error() calls stand out. I ended up monkeypatching the logging module by dropping this in my main.py as a quick solution:

# monkey patch logger to dump ERRORs in red
import os
if os.environ['SERVER_SOFTWARE'].find('Development') >= 0:
    import logging
    old_error = logging.error
    def red_error(msg,*args,**kwargs):
        old_error("\033[22;31m%s\033[0;0m" % msg, *args, **kwargs)
    logging.error = red_error

This will only for on ANSI-color terminals.

like image 5
Chris Farmiloe Avatar answered Nov 09 '22 17:11

Chris Farmiloe