Skip to content

Instantly share code, notes, and snippets.

@HarasimowiczKamil
Created July 31, 2019 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HarasimowiczKamil/1c958438d30d1a6f9b0fa6688c2252c2 to your computer and use it in GitHub Desktop.
Save HarasimowiczKamil/1c958438d30d1a6f9b0fa6688c2252c2 to your computer and use it in GitHub Desktop.
# functions
CLREOL=$'\x1B[K'
# ex. tail-color "/path/to/logs.log" "PHRASE TO EXTRA COLOR"
tail-color () {
COLORS="-e 's/\(.*DEBUG.*\)/\o033[32m\1\o033[39m/'"
COLORS="$COLORS -e 's/\(.*NOTICE.*\) /\o033[97m\1\o033[39m/'"
COLORS="$COLORS -e 's/\(.*WARNING.*\) /\o033[93m\1\o033[39m/'"
COLORS="$COLORS -e 's/\(.*ERROR.*\)/\o033[91m\1\o033[39m/'"
COLORS="$COLORS -e 's/\(.*\(ALERT|EMERGENCY\).*\) /\o033[31m\1\o033[39m/'"
COLORS="$COLORS -e 's/\(.*CRITICAL.*\)/\o033[41m\1\o033[49m/'"
COLORS="$COLORS -e 's/\($2\)/\o033[36m\1\o033[39m/'"
CMD="tail -n 100 --pid=$BASHPID -f $1 | sed --unbuffered $COLORS"
echo "$CMD"
eval ${CMD}
}
# ex. rotate "/path/to/logs.log"
rotate () {
TAILPID=0
function ctrl_c() {
echo "** [Ctrl+C]: kill tail process $TAILPID"
kill $TAILPID 2>/dev/null
}
echo "Pattern: $1"
trap ctrl_c INT
CURRENT=null
while true; do
NEWLOG=$(eval "echo $1")
if [[ $NEWLOG != $CURRENT && -e "$NEWLOG" ]]
then
kill $TAILPID 2>/dev/null
CURRENT=$NEWLOG
echo -e "\e[44m==================== $CURRENT ====================${CLREOL}\e[49m"
echo -e "\e[44mtail-color $CURRENT $2 &${CLREOL}"
tail-color $CURRENT $2 &
echo -e "\e[44m==================== $CURRENT ====================${CLREOL}\e[49m"
TAILPID=$!
fi
sleep 1
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment