UpdateController/log_helpers

84 lines
1.3 KiB
Plaintext
Raw Normal View History

2022-06-02 15:27:37 +02:00
#!/bin/bash
# set -x
2022-06-03 20:45:52 +02:00
#if [ ${log_helpers_sourced:-1} = "1" ]; then
# readonly log_helpers_sourced=${BASH_SOURCE[0]}
#else
# return 0
#fi
readonly log_file=/var/log/update_controller.log
if ! [ -f "$log_file" ]; then
touch $log_file
fi
2022-06-02 15:27:37 +02:00
readonly DEBUG=0
readonly INFO=1
readonly WARN=2
readonly CRIT=3
2022-06-03 20:45:52 +02:00
readonly ERROR=4
readonly FATAL=5
readonly MAX_DEBUG_LEVEL=6
2022-06-02 15:27:37 +02:00
log_level=0
set_dbg_level () {
if [ $1 < $MAX_DEBUG_LEVEL ]; then
log_level=$1
fi
}
dbg_level () {
return $log_level
}
log() {
2022-06-03 20:45:52 +02:00
if [[ $(("$(wc -l < $log_file)")) -ge $((100000)) ]]; then
2022-06-02 15:27:37 +02:00
# remove first line
sed -e 1d -i $log_file
fi
local msg="$(date +'%Y-%m-%d_%T'): $*"
2022-06-03 20:45:52 +02:00
printf "log:$msg\n" >&2
2022-06-02 15:27:37 +02:00
printf "$msg\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
}
2022-06-03 20:45:52 +02:00
log_error() {
if [ $log_level -le $ERROR ]; then
log "ERROR $*"
fi
}
2022-06-02 15:27:37 +02:00
log_fatal() {
if [ $log_level -le $FATAL ]; then
log "FATAL $*"
log "exiting ..."
exit 1
fi
}
# log "test message1" "test message2"