28th October, 2008

Easy to read log files

When dealing with a large codebase, or any codebase really, debug log files can get pretty detailed. I made the mistake of not separating my log entries into degrees of importance with a switch in some settings file, so I basically have logging enabled or disabled. When logging is disabled pages render between 8 and 35 ms, when enabled pages can take up to 500 ms.

The problem?

Giant log files for one page refresh. The more unformatted text you have in a block passing by the less readable you make it and the more likely you are to notice problems.

The solution?

Format those files!

Colour is the perfect way to highlight certain important sections in a log. If you're watching a file with tail in a console, you simply need to slip some ANSI codes in to change the colours of each entry.

Let's begin some fun code!

class Log(self):
    LogColour = { 
        "Black" : "\033[0;30m",
        "Dark Gray" : "\033[0;30m",
        "Purple" : "\033[0;35m",
        "Blue" : "\033[0;34m",
        "Light Blue" : "\033[0;34m",
        "Green" : "\033[0;32m",
        "Light Green" : "\033[1;32m",
        "Cyan" : "\033[0;36m",
        "Light Cyan" : "\033[0;36m",
        "Red" : "\033[0;31m",
        "Light Red" : "\033[0;31m",
        "Purple" : "\033[0;35m",
        "Light Purple" : "\033[0;35m",
        "Brown" :"\033[0;33m",
        "Yellow" : "\033[0;33m",
        "Light Gray" : "\033[0;37m",
        "White" : "\033[0m"
    }

    LogColourWord = {
        "initiali" : "Light Green",
        "start" : "Light Green",
        "exception" : "Light Red",
        "success" : "Green",
        "true" : "Green",
        "done" : "Green"
    }

    def Write(self, text):
        # we need to run a quick comparison on the incoming entry
        lower_text = str(text.lower())
        colour = LogColour["White"]
        for loop_word, loop_colour in LogColourWord.items():
            if lower_text.find(loop_word) != -1:
                colour = LogColour[loop_colour]
        text = colour + text

        logFile = open("log.txt", "wt")
        logFile.write(text)
        logFile.close()

How it works:

We set up a dictionary LogColour to map colour names to the ANSI codes. Then we create a lookup LogColourWord which maps text matches to the colour constant.

The Write function takes a log line item and looks for words that match in the lookup for a meaningful context. If the line item contains "success" or "true" colour it green. If it contains "exception" colour it red.

After implementing this small enhancement I'm sure your logs will become easier to read.

Looks good and tastes nice, too!

Your logs could look like this

 

The opinions expressed here are my own and not those of my employer.