Always log to file. Log to terminal depending on debug level.

This commit is contained in:
Gerhard Hoffmann 2023-05-24 13:02:56 +02:00
parent 33511a5bcb
commit 4f9c6155e1

View File

@ -1,10 +1,6 @@
#!/bin/bash
# set -x
RED='\e[0;31m'
GREEN='\e[0;32m'
NC='\e[0m' # No Color
if [ "${log_helpers_sourced:-1}" = "1" ]; then # include only once
readonly log_helpers_sourced=${BASH_SOURCE[0]}
@ -33,51 +29,60 @@ if [ "${log_helpers_sourced:-1}" = "1" ]; then # include only once
return $log_level
}
log() {
# TODO: kann man wohl entfernen
log_to_file() {
if [[ $(("$(wc -l < $log_file)")) -ge $((100000)) ]]; then
# remove first line
sed -e 1d -i $log_file
fi
local msg="$(date +'%Y-%m-%d_%T'): $*"
echo "log:$msg" >&2
echo "$msg" >> $log_file
}
log() {
local msg="$(date +'%Y-%m-%d_%T'): $*"
echo "log:$msg" >&2
}
log_debug() {
log_to_file "DEBUG $*"
if [ $log_level -le $DEBUG ]; then
log "DEBUG $*"
fi
}
log_info() {
log_to_file "INFO $*"
if [ $log_level -le $INFO ]; then
log "${GREEN}INFO $*"
log "INFO $*"
fi
}
log_warn() {
log_to_file "WARN $*"
if [ $log_level -le $WARN ]; then
log "${RED}WARN $*"
log "WARN $*"
fi
}
log_crit() {
log_to_file "CRIT $*"
if [ $log_level -le $CRIT ]; then
log "${RED}CRIT $*"
log "CRIT $*"
fi
}
log_error() {
log_to_file "ERROR $*"
if [ $log_level -le $ERROR ]; then
log "${RED}ERROR $*"
log "ERROR $*"
fi
}
log_fatal() {
log_to_file "FATAL $*"
if [ $log_level -le $FATAL ]; then
log "${RED}FATAL $*"
log "${RED}exiting ..."
log "FATAL $*"
log "exiting ..."
exit 1
fi
}