Add colors to your logs

When tailing a log file, it can look like a sea of black and white and everything blurs together. Here’s something really easy to put some color in your logs to help identify what you are looking for.

tail -F -n5000 /opt/openhab2/userdata/logs/openhab.log | sed --unbuffered -e 's/\(.*\[ERROR.*\)/\o033[1;31m\1\o033[0;39m/' -e 's/\(.*\[WARN.*\)/\o033[1;33m\1\o033[0;39m/' -e 's/\(.*\[INFO.*\)/\o033[1;32m\1\o033[0;39m/' -e 's/\(.*\[DEBUG.*\)/\o033[1;34m\1\o033[0;39m/' -e 's/\(.*\[TRACE.*\)/\o033[1;35m\1\o033[0;39m/'

image

If you are not using a manual install of OH, you can use this for the path to the openhab.log file…

${OPENHAB_USERDATA}/logs/openhab.log

I run OH as a service, but I have a script that I run after a restart of the server that includes setting up screens for tailing individual log files that I have created appenders for. This lets the logging run in the background on the server and I can access it from anywhere I can ssh in to the server. Here is a smaller version of that script…

#!/bin/bash
echo "Starting screens with logs tailing ..."

screen -d -m -S openhab

colors="sed --unbuffered \
-e 's/\\(.*\\[ERROR.*\\)/\\o033[1;31m\\\1\\o033[0;39m/' \
-e 's/\\(.*\\[WARN.*\\)/\\o033[1;33m\\\1\\o033[0;39m/' \
-e 's/\\(.*\\[INFO.*\\)/\\o033[1;32m\\\1\\o033[0;39m/' \
-e 's/\\(.*\\[DEBUG.*\\)/\\o033[1;34m\\\1\\o033[0;39m/' \
-e 's/\\(.*\\[TRACE.*\\)/\\o033[1;35m\\\1\\o033[0;39m/'"

screen -d -m -S openhablog
screen -S openhablog -p 0 -X stuff "tail -F -n5000 /opt/openhab2/userdata/logs/openhab/openhab.log | ${colors}$(printf \\r)"

screen -d -m -S rules
screen -S rules -p 0 -X stuff "tail -F -n5000 /opt/openhab2/userdata/logs/rules/rules.log | ${colors}$(printf \\r)"

screen -d -m -S jsr223
screen -S jsr223 -p 0 -X stuff "tail -F -n5000 /opt/openhab2/userdata/logs/jsr223/jsr223.log | ${colors} \
-e 's/\(.*ScriptEngineManagerImpl.*\)/\o033[1;31m\1\o033[0;39m/'$(printf \\r)"

screen -d -m -S zigbee
screen -S zigbee -p 0 -X stuff "tail -F -n5000 /opt/openhab2/userdata/logs/zigbee/zigbee.log | ${colors}$(printf \\r)"

screen -d -m -S zwave
screen -S zwave -p 0 -X stuff "tail -F -n5000 /opt/openhab2/userdata/logs/zwave/zwave.log | ${colors}$(printf \\r)"
13 Likes

Very creative! I had never seen send used this way I like it!

Thanks, just added it to a script for tailing my logs

What can this script do? Do i have tabs inside my frontail tail log or will there be all in one tail and only the colors will change?

I can open my frontail with this url: http://192.168.1.1:9001/

What will this script do in my case? Why not only change the frontail config-file instead of using your script?

For more information about screen check this page:

TL;DR
Screen allows you to setup an SSH connection, disconnect from it (either intentional or due to communication error) and reattach to the screen session. Very useful when running lengthy operations on a remote system like a tail on a log file or a remote system upgrade.

1 Like

Ok, thanks. So this is not for showing my log with logtail on my browser. Is there also a way to get coloured lines inside frontail?

If you’re using openhabian then this is already working out of the box. If not, then you can find the required files here:

frontail-preset.json
frontail-theme.css

To run frontail as a service on Linux
frontail.service

The service starts tailing both the openhab.log and event.log file:

/usr/bin/frontail --ui-highlight --ui-highlight-preset /usr/lib/node_modules/frontail/preset/openhab.json -t openhab -l 2000 -n 200 /var/log/openhab2/openhab.log /var/log/openhab2/events.log

3 Likes

Nice! I like how it works without extra tools.

I use multitail with Christoph’s color coding scheme for openHAB to do much the same thing. I like multitail because it supports tailing more than one file and you can tail a file on another machine. the color coding isn’t as dramatic as coloring the whole line though which could be handy in a number of situations. I’ll be bookmarking this post for sure.

Now to figure out that error…

Sorry to revive an old post, but I thought someone may land here (as I did) and could be interested in a possible solution.

As you, I was wondering how to deal with the colouring inside Frontail, and I found this thread. Thanks to @5iver and @marcel_erkel, who provided the first info I needed to start, I messed around a bit with the json and css files.

In case you (or anyone else) are interested, I went a bit further and documented what i did in this post.