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
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With