From 8763eccab38583d3ecacf1053c8639e61cd55cd5 Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Thu, 2 Jun 2022 15:27:37 +0200 Subject: [PATCH] removed sh-suffix --- log_helpers | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100755 log_helpers diff --git a/log_helpers b/log_helpers new file mode 100755 index 0000000..274294e --- /dev/null +++ b/log_helpers @@ -0,0 +1,77 @@ +#!/bin/bash +# set -x + +if [ ${log_helpers_sourced:-1} = "1" ]; then + readonly log_helpers_sourced=${BASH_SOURCE[0]} +else + printf "$loh_helpers_sourced already sourced\n" &>2 + return 0 +fi + +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() { + local log_file=/var/log/update_controller.log + if [ -f "$log_file" ]; then + touch $log_file + fi + + if [[ "$(wc -l < $log_file)" > "100000" ]]; then + # remove first line + sed -e 1d -i $log_file + fi + local msg="$(date +'%Y-%m-%d_%T'): $*" + printf "$msg\n" >&2 + 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 +} + +log_fatal() { + if [ $log_level -le $FATAL ]; then + log "FATAL $*" + log "exiting ..." + exit 1 + fi +} + +# log "test message1" "test message2"