Skip to content

Instantly share code, notes, and snippets.

@klashxx
Created August 8, 2016 12:38
Show Gist options
  • Save klashxx/f833442afda82b91f77bd26718a9b5c4 to your computer and use it in GitHub Desktop.
Save klashxx/f833442afda82b91f77bd26718a9b5c4 to your computer and use it in GitHub Desktop.
declare -a loggers
declare -A fileConfig
declare -A streamConfig
declare -A LEVELS=(["DEBUG"]=10 ["INFO"]=20 ["WARNING"]=30
["ERROR"]=40 ["CRITICAL"]=50)
readonly LEVELS
function log {
declare -a comp
local level=$1
local line=$2
shift 2
for logger in "${loggers[@]}"; do
cnflevel="$(eval echo \${${logger}Config[level]})"
(( ${LEVELS[${level}]} < ${LEVELS[${cnflevel}]} )) && continue
datefmt="$(eval echo \${${logger}Config["datefmt"]})"
[ $datefmt ] && { local time_stamp=$(date +"${datefmt}"); comp+=("${time_stamp}"); }
format="$(eval echo \${${logger}Config["format"]})"
comp+=("${level}")
if [ $logger = file ]; then
comp+=("${line}")
exec 6>&1
exec >>${fileConfig[filename]}
fi
printf "${format}" ${comp[@]} "$@"
if [ $logger = file ]; then
exec 1>&6 6>&-
fi
unset comp format datefmt time_stamp
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment