Last active
May 19, 2023 15:51
-
-
Save mrsarm/3e6f9d5fbbd1cc908deee3a1b13482a5 to your computer and use it in GitHub Desktop.
taillog: tail FILE in --follow mode highlighting with colors the severity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# taillog: tail FILE highlighting with colors the severity. | |
# | |
# Based in the script found at https://serverfault.com/a/1022806/256983 | |
# | |
# TODO: check whether the syntax "\<MESSAGE\>" offers better compatibility than "\bMESSAGE\b" to match whole words only (Mac OS) | |
if [ "$1" = "-h" -o "$1" = "--help" ] | |
then | |
echo -e "\033[1;1mtaillog\033[0m: tail FILE in --follow mode highlighting with colors the severity: \033[1;36mdebug\033[0m, \033[1;32minfo\033[0m, \033[1;33mwarn\033[0m, \033[1;31merror\033[0m or \033[1;31mfatal\033[0m." | |
echo | |
echo -e "\033[1;1mUsage: taillog [FILE]\033[0m" | |
echo | |
echo "If FILE is not provided, it tails the standard input." | |
echo | |
echo -e "If \033[1;1m\$GREP_ARGS\033[0m is set, it filters with grep the output with the given arguments and show the results in \033[1;35mpurple\033[0m." | |
echo | |
echo "E.g. to filter only logs from Postgres you might use:" | |
echo | |
echo " $ GREP_ARGS=\"^postgres:\" taillog file.log" | |
echo | |
echo "The opposite filter to NOT get the logs from Postgres might be:" | |
echo | |
echo " $ GREP_ARGS=\"-v ^postgres:\" taillog file.log" | |
echo | |
echo -e "You can also override the default arguments passed to tail (\`-f -n 100') with the env variable \033[1;1m\$TAIL_ARGS\033[0m." | |
exit | |
fi | |
shopt -s expand_aliases | |
alias filter-grep="GREP_COLOR='1;35' grep -i -E --color=always --line-buffered" | |
alias grey-grep="GREP_COLOR='1;30' grep -i -E --color=always --line-buffered" | |
alias red-grep="GREP_COLOR='1;31' grep -i -E --color=always --line-buffered" | |
alias green-grep="GREP_COLOR='1;32' grep -i -E --color=always --line-buffered" | |
alias yellow-grep="GREP_COLOR='1;33' grep -i -E --color=always --line-buffered" | |
alias cyan-grep="GREP_COLOR='1;36' grep -i -E --color=always --line-buffered" | |
if [ -z "${TAIL_ARGS+x}" ] | |
then | |
TAIL_ARGS="-f -n 100" | |
fi | |
if [ -z "$GREP_ARGS" ] | |
then | |
tail $TAIL_ARGS $1 | cyan-grep "\bdebug\b|$" | green-grep "\binfo\b|$" | yellow-grep "\bwarn\b|\bwarning\b|$" | red-grep "\berror\b|\bfatal\b|\bcritical\b|\bCommandError\b|\bTimeoutError\b|\bexception\b|\bTraceback\b|$" | |
else | |
tail $TAIL_ARGS $1 | filter-grep $GREP_ARGS | cyan-grep "\bdebug\b|$" | green-grep "\binfo\b|$" | yellow-grep "\bwarn\b|\bwarning\b|$" | red-grep "\berror\b|\bfatal\b|\bcritical\b|\bCommandError\b|\bTimeoutError\b|\bexception\b|\bTraceback\b|$" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment