Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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
You can’t perform that action at this time.