At Wix we produce gigabytes of logs for different applications every day. Logs are not the only way we monitor our systems, but they play an important role, both for troubleshooting purposes and to check the general health of an application.
One of the problems we had was the readability of logs. When you know what you’re looking for in a log, it is easy to search for an exception. But when you don’t know what is wrong, going through gigabytes of logs in white text on a black background can be very time consuming and onerous.
Our solution was to add colors to our logs to facilitate the readability. Wouldn’t it be great if every level (i.e., info, warning, error) message had a different color? What if we could have different colors in our stack trace to highlight different exceptions?
Before jumping into the details of the implementation, let’s first understand how the coloring works in Unix.
An easy way to understand it is to type the following in a Unix terminal:
echo -e "\033[31mException log\033[39m"
The result is:
First, it’s important to know that every color has a corresponding ANSI color code:
Black = 30
Red = 31
Green = 32
Yellow = 33
Default = 39
For the color codes to take effect, they need to be preceded by the “start” escape character, \033[, and be followed by the “end” escape character, m.
Therefore, to display a message in red, we would type the following:
We then need to set the color back to the default color; otherwise, everything in the terminal will be displayed in red from that point on.
To do this, we would type:
Fortunately we are using Logback to produce our logs, which provides an easy way to add colors: http://logback.qos.ch/manual/layouts.html.
It is also possible to use the existing %highlight keyword to display messages in different colors depending on the error level.
We want to have our logs colored in a very specific way, so we’ll need to create our own keyword. This keyword will be linked to a customize class that will contain our specific logic.
A stack trace consists of different lines, all corresponding to classes with their full package. We would like to color these lines depending on their corresponding packages. Some lines don’t need to be colored at all.
This is our full implementation using Scala:
Line 12: Class StracktraceLogHighlighter extends abstract class CompositeConverter.
Line 14: Override the abstract method transform.
Lines 14-21: Loop through each line in the stack trace. For each line, call method lineColouring.
Lines 23-29: Lines will be analyzed here using Scala Extractors. The Extractors will output the color code (e.g., 30, 31, etc.) for a given line. If none of the Extractors returns a value, then the line will be displayed without a color.
Lines 31-34: Implementation of ImportantWixLine Extractor. If the line contains “com.wixpress.framework.” or “com.wixpress.hoopoe.”, it returns color code Magenta (35).
Lines 36-39: Implementation of WixLine Extractor. If the line contains “com.wix”, it returns color code Yellow (33).
Lines 41-42: This method will display the given line in a color (see example above for detailed explanation). It is called for each line in the lineColouring method.
We can now link this class with our new stacktraceHighlight keyword in the logback.xml file:
The encoder part in our appender will look like:
The following commands can be used in order to see the colors displayed properly in the logs:
tail, more, cat, or less -R. The -R option causes “raw” control characters to be displayed.
When opening this log with vi, we can see the color codes around each line:
I hope you enjoy your colorful logs!