Coloured logs are very helpful especially when you are going to deploy your application in production. The moment when the live traffic is shifted to your servers and you sit there eagerly watching the logs roll off in speed, only one thing in your mind that no warnings and errors should go by unnoticed. This is the best situation where colors play an important role.
In this article, I’m going to show you a way by which you can grep the individual lines from the logs using regex pattern match and change the color of that line instantaneously.
Let’s get started…
ITerm2 Triggers
In iTerm, a trigger is an action that is performed when text matching any regular regex pattern is received in the terminal session.
So, you’ll be creating triggers for different regex patterns which are used in the logs. Usually, loggers in Java follow a pattern where for every error they print out “error” keyword in the logs and likewise “warn” for warning and “Fatal” if something terrible has happened 🙂
I will show you how to create triggers for the above keywords, you can modify the regex to match the keyword that is getting used in your application while printing logs.
A dialog box will open.
Click on the “Profiles” tab and select “Advanced” at the right.
This is where you will find the option for editing the triggers. Click on the “edit” in the triggers section and create triggers as below.
I’ve created triggers for error, warn and fatal regex.
Once you have created the triggers, the next time your terminal shows up the word error, it will show that line as red.
Conclusion
This is a very cool feature offered by iTerm terminal. I use iTerm2 as my default terminal on mac. And always make sure you are using the coloured logs as it makes it so much easier to view and identify if something has gone wrong.