How does one make logging color in Django/Google App Engine?
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',
},
}
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: