UpdateController/log_helpers

86 lines
1.7 KiB
Plaintext
Raw Normal View History

2022-06-02 15:27:37 +02:00
#!/bin/bash
# set -x
2023-03-31 09:45:57 +02:00
RED='\e[0;31m'
GREEN='\e[0;32m'
NC='\e[0m' # No Color
2022-06-02 17:27:30 +02:00
if [ "${log_helpers_sourced:-1}" = "1" ]; then # include only once
readonly log_helpers_sourced=${BASH_SOURCE[0]}
2022-06-02 15:27:37 +02:00
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
readonly ERROR=4
readonly FATAL=5
readonly MAX_DEBUG_LEVEL=6
2022-06-02 15:27:37 +02:00
2022-07-08 13:50:16 +02:00
log_level=$INFO
2022-06-02 15:27:37 +02:00
set_dbg_level () {
if [ $1 < $MAX_DEBUG_LEVEL ]; then
log_level=$1
fi
}
2022-06-02 15:27:37 +02:00
dbg_level () {
return $log_level
}
2022-06-02 15:27:37 +02:00
log() {
2022-07-08 13:50:16 +02:00
# TODO: kann man wohl entfernen
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
}
2022-06-02 15:27:37 +02:00
log_debug() {
2022-07-08 13:50:16 +02:00
if [ $log_level -le $DEBUG ]; then
log "DEBUG $*"
fi
}
2022-06-02 15:27:37 +02:00
log_info() {
if [ $log_level -le $INFO ]; then
2023-03-31 09:45:57 +02:00
log "${GREEN}INFO $*"
fi
}
2022-06-02 15:27:37 +02:00
log_warn() {
if [ $log_level -le $WARN ]; then
2023-03-31 09:45:57 +02:00
log "${RED}WARN $*"
fi
}
2022-06-02 15:27:37 +02:00
log_crit() {
if [ $log_level -le $CRIT ]; then
2023-03-31 09:45:57 +02:00
log "${RED}CRIT $*"
fi
}
2022-06-03 20:45:52 +02:00
log_error() {
if [ $log_level -le $ERROR ]; then
2023-03-31 09:45:57 +02:00
log "${RED}ERROR $*"
fi
}
2022-06-02 15:27:37 +02:00
log_fatal() {
if [ $log_level -le $FATAL ]; then
2023-03-31 09:45:57 +02:00
log "${RED}FATAL $*"
log "${RED}exiting ..."
exit 1
fi
}
###############################################################################
fi ### include guard