Advertisement
rockdrilla

logging facility, rev.2

Oct 29th, 2013
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.02 KB | None | 0 0
  1. # place @ start of script for easy modification :)
  2. # also, you're welcome to override
  3. : ${LOG_LEVEL:=4} # read function log() @ <logging facility>
  4. : ${LOG_FD:=2} # 1 = stdout, 2 = stderr
  5.  
  6. # ....
  7.  
  8. # <logging_facility>
  9. clr_nc=$'\e[0m'
  10. clr_red=$'\e[1;31m'
  11. clr_green=$'\e[1;32m'
  12. clr_yellow=$'\e[1;33m'
  13. clr_cyan=$'\e[1;36m'
  14. log() {
  15.     local L=''
  16.     [ $# -gt 0 ] && L=$1 && shift
  17.     L=${L:-'U'} # 'U' means 'unclassified'
  18.  
  19.     local c0=${clr_nc} c1=''
  20.     case "$L" in
  21.     E) test ${LOG_LEVEL} -lt 1 && return 0; c1=${clr_red} ;;
  22.     W) test ${LOG_LEVEL} -lt 2 && return 0; c1=${clr_yellow} ;;
  23.     I) test ${LOG_LEVEL} -lt 3 && return 0; c1=${clr_green} ;;
  24.     D) test ${LOG_LEVEL} -lt 4 && return 0; c1=${clr_cyan} ;;
  25.     *) test ${LOG_LEVEL} -lt 4 && return 0; c1=${clr_nc} ;;
  26.     esac
  27.     [ ! -t ${LOG_FD} ] && c0='' && c1=''
  28.  
  29.     {
  30.         [ $# -gt 0 ] \
  31.         && echo "[${c1}$L${c0}] ${c1}$@${c0}" \
  32.         || sed -re "s#^(.*)\$#[${c1}$L${c0}] ${c1}\\1${c0}#"
  33.     } >&${LOG_FD}
  34. }
  35. # </logging_facility>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement