views:

376

answers:

4

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

+1  A: 

The reset codes mentioned in the answer you linked to will work on a console in the local development server (but will likely take some tweaking - you'll have to chain it with the existing App Engine logging handler), but won't work in production, since in production log entries are output to an HTML page in your admin console.

You can, however, filter by log level in the admin console.

Nick Johnson
Thanks for answering, Nick. It's the chaining part that I'm having trouble with. :)
Brian M. Hunt
+2  A: 

I don't believe that you should create a logger subclass just for this - airmind's answer is fine as far as creating a specialised Formatter and specifying its use on a StreamHandler. But there's no need for a logger subclass. In fact airmind's use of a logger class adds a handler to every logger created, which is not what you want.

The solution airmind gave only works for terminals which support ANSI escape sequences - are you sure that your console does support them?

Vinay Sajip
Thanks Vinay. Yes, my terminal supports ANSI colors.
Brian M. Hunt
A: 

Here is a sample formater:

class Formatter(logging.Formatter) :
    _level_colors  = {
      "DEBUG": "\033[22;32m", "INFO": "\033[01;34m",
      "WARNING": "\033[22;35m", "ERROR": "\033[22;31m",
      "CRITICAL": "\033[01;31m"
     };    

    def format(self, record):
        if(Formatter._level_colors.has_key(record.levelname)):
            record.levelname = "%s%s\033[0;0m" % \
                            (Formatter._level_colors[record.levelname],
                             record.levelname)
        record.name = "\033[37m\033[1m%s\033[0;0m" % record.name
        return logging.Formatter.format(self, record)    

You need to configure it, for example:

...
[formatters]
keys=console_formatter
...
[handler_console_handler]
class=StreamHandler
formatter=console_formatter
args=(sys.stdout,)
maciejka
+1  A: 

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.

ozone