168 lines
4.3 KiB
Bash
168 lines
4.3 KiB
Bash
#!/bin/bash
|
|
#### LOGGING
|
|
## from https://www.cubicrace.com/2016/03/log-tracing-mechnism-for-shell-scripts.html
|
|
[ -z "${SCRIPT_LOG+x}" ] && export SCRIPT_LOG=${HOME}/logs/scriptlog
|
|
mkdir -p "$(dirname ${SCRIPT_LOG})"
|
|
touch $SCRIPT_LOG
|
|
|
|
|
|
function SCRIPTENTRY(){
|
|
local loglevels
|
|
declare -a loglevels
|
|
loglevels=("ERROR" "WARN" "INFO")
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
|
|
timeAndDate=`date`
|
|
script_name=`basename -- "$0"`
|
|
script_name="${script_name%.*}"
|
|
echo "[$timeAndDate] [DEBUG] > $script_name $FUNCNAME" >> $SCRIPT_LOG
|
|
fi
|
|
}
|
|
|
|
function SCRIPTEXIT(){
|
|
local loglevels
|
|
declare -a loglevels
|
|
loglevels=("ERROR" "WARN" "INFO")
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
|
|
script_name=`basename -- "$0"`
|
|
script_name="${script_name%.*}"
|
|
echo "[$timeAndDate] [DEBUG] < $script_name $FUNCNAME" >> $SCRIPT_LOG
|
|
fi
|
|
}
|
|
|
|
function ENTRY(){
|
|
local loglevels
|
|
declare -a loglevels
|
|
loglevels=("ERROR" "WARN" "INFO")
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
|
|
local cfn="${FUNCNAME[1]}"
|
|
timeAndDate=`date`
|
|
echo "[$timeAndDate] [DEBUG] > $cfn $FUNCNAME" >> $SCRIPT_LOG
|
|
fi
|
|
}
|
|
|
|
function EXIT(){
|
|
local loglevels
|
|
declare -a loglevels
|
|
loglevels=("ERROR" "WARN" "INFO")
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
|
|
local cfn="${FUNCNAME[1]}"
|
|
timeAndDate=`date`
|
|
echo "[$timeAndDate] [DEBUG] < $cfn $FUNCNAME" >> $SCRIPT_LOG
|
|
fi
|
|
}
|
|
|
|
function logerror(){
|
|
local function_name="${FUNCNAME[1]}"
|
|
case $1 in
|
|
-n)
|
|
local opts="-n"
|
|
shift
|
|
;;
|
|
esac
|
|
local msg="$1"
|
|
timeAndDate=`date`
|
|
local loglevels
|
|
declare -a loglevels
|
|
loglevels=()
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
|
|
echo $opts "$msg" >&2
|
|
fi
|
|
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL-$FILELOGLEVEL_DEFAULT}" ]];then
|
|
echo "[$timeAndDate] [ERROR] $msg" >> $SCRIPT_LOG
|
|
fi
|
|
}
|
|
|
|
function logwarn(){
|
|
local function_name="${FUNCNAME[1]}"
|
|
case $1 in
|
|
-n)
|
|
local opts="-n"
|
|
shift
|
|
;;
|
|
esac
|
|
local msg="$1"
|
|
timeAndDate=`date`
|
|
local loglevels
|
|
declare -a loglevels
|
|
loglevels=("ERROR" )
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
|
|
echo $opts "$msg" >&2
|
|
fi
|
|
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL-$FILELOGLEVEL_DEFAULT}" ]];then
|
|
echo "[$timeAndDate] [WARN] $msg" >> $SCRIPT_LOG
|
|
fi
|
|
}
|
|
|
|
function loginfo(){
|
|
local function_name="${FUNCNAME[1]}"
|
|
case $1 in
|
|
-n)
|
|
local opts="-n"
|
|
shift
|
|
;;
|
|
esac
|
|
local msg="$1"
|
|
timeAndDate=`date`
|
|
local loglevels
|
|
declare -a loglevels
|
|
loglevels=("ERROR" "WARN" )
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
|
|
echo $opts "$msg" >&2
|
|
fi
|
|
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL-$FILELOGLEVEL_DEFAULT}" ]];then
|
|
echo "[$timeAndDate] [INFO] $msg" >> $SCRIPT_LOG
|
|
fi
|
|
}
|
|
|
|
|
|
function logdebug(){
|
|
local function_name="${funcname[1]}"
|
|
case $1 in
|
|
-n)
|
|
local opts="-n"
|
|
shift
|
|
;;
|
|
esac
|
|
local msg="$1"
|
|
timeAndDate=`date`
|
|
local loglevels
|
|
declare -a loglevels
|
|
loglevels=("ERROR" "WARN" "INFO")
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
|
|
echo $opts "$msg" >&2
|
|
fi
|
|
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL-$FILELOGLEVEL_DEFAULT}" ]];then
|
|
echo "[$timeAndDate] [DEBUG] $msg" >> $SCRIPT_LOG
|
|
fi
|
|
}
|
|
|
|
function logtrace(){
|
|
local function_name="${funcname[1]}"
|
|
case $1 in
|
|
-n)
|
|
local opts="-n"
|
|
shift
|
|
;;
|
|
esac
|
|
local msg="$1"
|
|
timeAndDate=`date`
|
|
local loglevels
|
|
declare -a loglevels
|
|
loglevels=("ERROR" "WARN" "INFO" "DEBUG")
|
|
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL-$LOGLEVEL_DEFAULT}" ]];then
|
|
echo $opts "$msg" >&2
|
|
fi
|
|
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL-$FILELOGLEVEL_DEFAULT}" ]];then
|
|
echo "[$timeAndDate] [TRACE] $msg" >> $SCRIPT_LOG
|
|
fi
|
|
}
|
|
|
|
export -f logerror
|
|
export -f logwarn
|
|
export -f loginfo
|
|
export -f logdebug
|
|
export -f logtrace
|
|
export -f SCRIPTENTRY
|
|
export -f ENTRY
|
|
export -f SCRIPTEXIT
|
|
export -f EXIT
|