simplify logging
This commit is contained in:
parent
c36907737a
commit
7708bc3315
1 changed files with 4 additions and 15 deletions
19
logging
19
logging
|
@ -5,46 +5,35 @@
|
||||||
mkdir -p "$(dirname ${SCRIPT_LOG})"
|
mkdir -p "$(dirname ${SCRIPT_LOG})"
|
||||||
touch $SCRIPT_LOG
|
touch $SCRIPT_LOG
|
||||||
|
|
||||||
|
|
||||||
#for level in emerg alert crit err warning notice info debug; do
|
|
||||||
|
|
||||||
|
|
||||||
for level in SCRIPTENTRY SCRIPTEXIT ENTRY EXIT; do
|
for level in SCRIPTENTRY SCRIPTEXIT ENTRY EXIT; do
|
||||||
printf -v functext -- '%s() {
|
printf -v functext -- '%s() {
|
||||||
local loglevels
|
local loglevels=(emerg alert crit err warning notice info)
|
||||||
declare -a loglevels
|
|
||||||
loglevels=(emerg alert crit err warning notice info)
|
|
||||||
timeAndDate=$(date)
|
timeAndDate=$(date)
|
||||||
script_name="${BASH_SOURCE[0]##*/}"
|
|
||||||
local LOGLEVEL=${LOGLEVEL,,}
|
local LOGLEVEL=${LOGLEVEL,,}
|
||||||
if [[ ! ${loglevels[@]} =~ "${LOGLEVEL:-${LOGLEVEL_DEFAULT,,}}" ]];then
|
if [[ ! ${loglevels[@]} =~ "${LOGLEVEL:-${LOGLEVEL_DEFAULT,,}}" ]];then
|
||||||
echo "[%s] »${script_name}« ######################################################" >&2
|
echo "[%s] »${BASH_SOURCE[0]##*/} (${FUNCNAME[1]}) « ######################################################" >&2
|
||||||
fi
|
fi
|
||||||
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then
|
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then
|
||||||
echo "[${timeAndDate}] [%s] ${script_name}" >> "${SCRIPT_LOG}"
|
echo "[${timeAndDate}] [%s] $(basename $0) (${FUNCNAME[1]})" >> "${SCRIPT_LOG}"
|
||||||
fi }' "${level}" "${level^^}" "${level^^}"
|
fi }' "${level}" "${level^^}" "${level^^}"
|
||||||
eval "$functext"
|
eval "$functext"
|
||||||
export -f "${level}"
|
export -f "${level}"
|
||||||
done
|
done
|
||||||
|
|
||||||
loglevels=(trace debug info notice warning err crit alert emerg)
|
loglevels=(trace debug info notice warning err crit alert emerg)
|
||||||
|
|
||||||
for level in ${loglevels[@]}; do
|
for level in ${loglevels[@]}; do
|
||||||
loglevels=( ${loglevels[@]/$level} )
|
loglevels=( ${loglevels[@]/$level} )
|
||||||
printf -v functext -- 'log%s() {
|
printf -v functext -- 'log%s() {
|
||||||
local msg="$@"
|
local msg="$@"
|
||||||
[ -z "${msg:+x}" ] && return 0
|
[ -z "${msg:+x}" ] && return 0
|
||||||
local loglevels
|
|
||||||
declare -a loglevels
|
|
||||||
loglevels=(%s)
|
loglevels=(%s)
|
||||||
timeAndDate=$(date)
|
timeAndDate=$(date)
|
||||||
script_name="${BASH_SOURCE[0]##*/}"
|
|
||||||
local LOGLEVEL=${LOGLEVEL,,}
|
local LOGLEVEL=${LOGLEVEL,,}
|
||||||
if [[ ! ${loglevels[@]} =~ "${LOGLEVEL:-${LOGLEVEL_DEFAULT,,}}" ]];then
|
if [[ ! ${loglevels[@]} =~ "${LOGLEVEL:-${LOGLEVEL_DEFAULT,,}}" ]];then
|
||||||
printf "[%%s] (%%s) %%s\n" %s "${FUNCNAME[1]}" "${msg}" >&2
|
printf "[%%s] (%%s) %%s\n" %s "${FUNCNAME[1]}" "${msg}" >&2
|
||||||
fi
|
fi
|
||||||
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then
|
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then
|
||||||
printf "[%%s] [%%s] (%%s) %%s\n" "$timeAndDate" %s "${FUNCNAME[1]}" "${msg}" >> "${SCRIPT_LOG}"
|
printf "[%%s] [%%s] %%s (%%s) %%s\n" "$timeAndDate" %s "$(basename $0)" "${FUNCNAME[1]}" "${msg}" >> "${SCRIPT_LOG}"
|
||||||
fi }' "$level" "$(echo ${loglevels[@]})" "${level^^}" "${level^^}"
|
fi }' "$level" "$(echo ${loglevels[@]})" "${level^^}" "${level^^}"
|
||||||
eval "$functext"
|
eval "$functext"
|
||||||
export -f "log${level}"
|
export -f "log${level}"
|
||||||
|
|
Loading…
Reference in a new issue