Added log-helpers for different debug levels

This commit is contained in:
Gerhard Hoffmann 2022-06-05 07:29:20 +02:00
parent 26b2fad1e6
commit ee1160755e

View File

@ -1,6 +1,25 @@
#!/bin/bash #!/bin/bash
# set -x # set -x
readonly DEBUG=0
readonly INFO=1
readonly WARN=2
readonly CRIT=3
readonly FATAL=4
readonly MAX_DEBUG_LEVEL=5
log_level=0
set_dbg_level () {
if [ $1 < $MAX_DEBUG_LEVEL ]; then
log_level=$1
fi
}
dbg_level () {
return $log_level
}
log() { log() {
local log_file=/var/log/update_controller.log local log_file=/var/log/update_controller.log
if [ -f "$log_file" ]; then if [ -f "$log_file" ]; then
@ -11,7 +30,40 @@ log() {
sed -e 1d -i $log_file sed -e 1d -i $log_file
fi fi
message="$(date +'%D_%T'): $*" message="$(date +'%D_%T'): $*"
printf "$message\n" printf "$message\n" >&2
printf "$message\n" >> $log_file printf "$message\n" >> $log_file
} }
log_debug() {
if [ $log_level = $DEBUG ]; then
log "DEBUG $*"
fi
}
log_info() {
if [ $log_level -le $INFO ]; then
log "INFO $*"
fi
}
log_warn() {
if [ $log_level -le $WARN ]; then
log "WARN $*"
fi
}
log_crit() {
if [ $log_level -le $CRIT ]; then
log "CRIT $*"
fi
}
log_fatal() {
if [ $log_level -le $FATAL ]; then
log "FATAL $*"
log "exiting ..."
exit 1
fi
}
# log "test message1" "test message2" # log "test message1" "test message2"