myshellconfig/logging
Jakobus Schürz 658b3f4bf6 improve logging
* add function to logoutput
2022-08-11 12:11:01 +02:00

173 lines
4.7 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] [SCRIPTENTRY] > $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] [SCRIPTEXIT] < $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] [ENTRY] > $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] [EXIT] < $cfn $FUNCNAME" >> $SCRIPT_LOG
fi
}
function logerror(){
local function_name="${FUNCNAME[1]}"
case $1 in
-n)
local opts="-n"
shift
;;
esac
local msg="$1"
[ -z "${msg:+x}" ] && return 0
timeAndDate=`date`
local loglevels
declare -a loglevels
loglevels=()
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
echo $opts "[ERROR] (${FUNCNAME[1]}) $msg" >&2
fi
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then
echo "[$timeAndDate] [ERROR] (${FUNCNAME[1]}) $msg" >> $SCRIPT_LOG
fi
}
function logwarn(){
local function_name="${FUNCNAME[1]}"
case $1 in
-n)
local opts="-n"
shift
;;
esac
local msg="$1"
[ -z "${msg:+x}" ] && return 0
timeAndDate=`date`
local loglevels
declare -a loglevels
loglevels=("ERROR" )
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
echo $opts "[WARN] (${FUNCNAME[1]}) $msg" >&2
fi
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then
echo "[$timeAndDate] [WARN] (${FUNCNAME[1]}) $msg" >> $SCRIPT_LOG
fi
}
function loginfo(){
local function_name="${FUNCNAME[1]}"
case $1 in
-n)
local opts="-n"
shift
;;
esac
local msg="$1"
[ -z "${msg:+x}" ] && return 0
timeAndDate=`date`
local loglevels
declare -a loglevels
loglevels=("ERROR" "WARN" )
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
echo $opts "[INFO] (${FUNCNAME[1]}) $msg" >&2
fi
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then
echo "[$timeAndDate] [INFO] (${FUNCNAME[1]}) $msg" >> $SCRIPT_LOG
fi
}
function logdebug(){
local function_name="${FUNCNAME[1]}"
case $1 in
-n)
local opts="-n"
shift
;;
esac
local msg="$1"
[ -z "${msg:+x}" ] && return 0
timeAndDate=`date`
local loglevels
declare -a loglevels
loglevels=("ERROR" "WARN" "INFO")
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
echo $opts "[DEBUG] (${FUNCNAME[1]}) $msg" >&2
fi
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then
echo "[$timeAndDate] [DEBUG] (${FUNCNAME[1]}) $msg" >> $SCRIPT_LOG
fi
}
function logtrace(){
local function_name="${FUNCNAME[1]}"
case $1 in
-n)
local opts="-n"
shift
;;
esac
local msg="$1"
[ -z "${msg:+x}" ] && return 0
timeAndDate=`date`
local loglevels
declare -a loglevels
loglevels=("ERROR" "WARN" "INFO" "DEBUG")
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
echo $opts "[TRACE] (${FUNCNAME[1]}) $msg" >&2
fi
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then
echo "[$timeAndDate] [TRACE] (${FUNCNAME[1]}) $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