-
-
Save jdanbrown/1381665 to your computer and use it in GitHub Desktop.
#!/bin/bash -eu | |
# | |
# Color log4j-style logs for easier visual parsing. | |
# | |
# Usage: | |
# tail foo.log | log-color | |
# run-service | log-color | |
black="` tput setaf 0; tput bold`" | |
red="` tput setaf 1; tput bold`" | |
green="` tput setaf 2; tput bold`" | |
yellow="` tput setaf 3; tput bold`" | |
blue="` tput setaf 4; tput bold`" | |
magenta="` tput setaf 5; tput bold`" | |
cyan="` tput setaf 6; tput bold`" | |
white="` tput setaf 7; tput bold`" | |
#off="` tput sgr0`" | |
off="` echo -ne "[m\x0f"`" # (Works better on both osx and linux...) | |
exec sed -r " | |
# Timestamps | |
s/^\[[0-9]{4}[^]]*\]/$black\\0$off/ | |
s/^[0-9]{4}-[0-9:,/ -]*/$black\\0$off/ | |
# Log levels | |
s/\\b(FATAL|CRITICAL)\\b/$magenta\\0$off/g | |
s/\\bERROR\\b/$red\\0$off/g | |
s/\\bWARN(ING)?\\b/$yellow\\0$off/g | |
s/\\bINFO\\b/$green\\0$off/g | |
s/\\bDEBUG\\b/$blue\\0$off/g | |
s/\\bTRACE\\b/$cyan\\0$off/g | |
# Grouped things | |
s/ \[[^]]+\] \S+ - /$black\\0$off/ | |
s/ \([a-zA-Z_][a-zA-Z0-9_$.:]*\)$/$black\\0$off/ | |
" |
Hi jdanbrown, thank you for sharing this script with us. It's short while very powerful.
And hello chilladelia. For the mac, you may install home brew first, then install gun-sed to replace the mac original sed.
Use command below:
brew install gnu-sed --with-default-names
After that, the log-color script can be used in mac os.
And for simplicity. I write a very simple script to easily use to monitor the input into a log4j log file.
Save the below code into a file e.g. sslog:
#! /bin/sh
echo $1 | xargs tail -f | log-color
Then copy both sslog and log-color into /usr/local/bin. Give both of them execute privileges:
chmod +x /usr/local/bin/sslog
chmod +x /usr/local/bin/log-color
Finally, you can monitor a log4j log file from everywhere by type the following command:
slog log4j.log
Have fun!
Thanks a lot, works like a charm, even piped behind a tail -f
through a kubectl exec
😄
I wanted to give your script a try, but I get a "sed: illegal option --r " exception on Mac OS. May you provide a version without the "- r" parameter? This would be very helpful. Your scrips seems to be the most promising as I have seen so far.