Função de Log Shell Script

#!/bin/bash
# gera log e imprime mensagens na tela
# log ['ok|erro|info'] 'mensagem'
function log() {

  DATA=$( date '+%d/%m/%Y %H:%M' )

  if [ "$1" == 'ok' ]; then
    echo -e "[ $DATA ] \e[32;1m[ OK ]\e[m $2" | tee -a /var/log/$( basename $0 ).log
  elif [ "$1" == 'erro' ]; then
    echo -e "[ $DATA ] \e[31;1m[ ERRO ]\e[m $2" | tee -a /var/log/$( basename $0 ).log
  elif [ "$1" == 'info' ]; then
    echo -e "[ $DATA ] \e[33;1m[ * ]\e[m $2" | tee -a /var/log/$( basename $0 ).log
  else
    echo "[ $DATA ] $1" | tee -a /var/log/$( basename $0 ).log
  fi
}
log 'sem cor'; log 'ok' 'amarelo'; log 'erro' 'vermelho'; log 'info' 'amarelo'
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s