diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..827be52 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*~ +log.txt \ No newline at end of file diff --git a/README.md b/README.md index b77ec26..12d2e6a 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,9 @@ An *extremely* lightweight logging framework for BASH scripts. Developed and tested on MacOS. This should work on any BASH platform. ## Log levels -It supports four levels, default being `INFO`. +It supports five levels, default being `INFO`. - ERROR > WARN > INFO > DEBUG + FATAL > ERROR > WARN > INFO > DEBUG ## Usage diff --git a/demo.sh b/demo.sh index 034e49c..31d1681 100755 --- a/demo.sh +++ b/demo.sh @@ -14,3 +14,6 @@ tlog error "Something terrible has happened" tlog debug "end of script" tlog info "Script completed, go home" + +tlog fatal "Fatal Warning" +tlog xyz "Wrong log level" diff --git a/tinylogger.bash b/tinylogger.bash index ae36cc8..06889a4 100644 --- a/tinylogger.bash +++ b/tinylogger.bash @@ -22,17 +22,33 @@ # THE SOFTWARE. # # Author: Nagarjuna Kumarappan +# +# 15.04.2021 +# +# Changes: +# - Log Level fatal added +# - Log output redirected to log file +# - shellcheck recommendations implemented +# - Default catch added +# +# Author: Markus Heene # defaults LOGGER_FMT=${LOGGER_FMT:="%Y-%m-%d %H:%M:%S"} -LOGGER_LVL=${LOGGER_LVL:="info"} +# Log messages with the priority from at least will be printed +LOGGER_LVL=${LOGGER_LVL:="info"} +LOG_FILE="log.txt" function tlog { + local action action=$1 && shift case $action in - debug) [[ $LOGGER_LVL =~ debug ]] && echo "$( date "+${LOGGER_FMT}" ) - DEBUG - $@" 1>&2 ;; - info) [[ $LOGGER_LVL =~ debug|info ]] && echo "$( date "+${LOGGER_FMT}" ) - INFO - $@" 1>&2 ;; - warn) [[ $LOGGER_LVL =~ debug|info|warn ]] && echo "$( date "+${LOGGER_FMT}" ) - WARN - $@" 1>&2 ;; - error) [[ ! $LOGGER_LVL =~ none ]] && echo "$( date "+${LOGGER_FMT}" ) - ERROR - $@" 1>&2 ;; + debug) [[ $LOGGER_LVL =~ debug ]] && echo "$( date "+${LOGGER_FMT}" ) - DEBUG - $*" >>"${LOG_FILE}" ;; + info) [[ $LOGGER_LVL =~ debug|info ]] && echo "$( date "+${LOGGER_FMT}" ) - INFO - $*" >>"${LOG_FILE}" ;; + warn) [[ $LOGGER_LVL =~ debug|info|warn ]] && echo "$( date "+${LOGGER_FMT}" ) - WARN - $*" >>"${LOG_FILE}" ;; + error) [[ ! $LOGGER_LVL =~ debug|info|warn|error ]] && echo "$( date "+${LOGGER_FMT}" ) - ERROR - $*" | tee -a "${LOG_FILE}" 1>&2 ;; + fatal) [[ ! $LOGGER_LVL =~ none ]] && echo "$( date "+${LOGGER_FMT}" ) - FATAL - $*" | tee -a "${LOG_FILE}" 1>&2 ;; + *) echo "$( date "+${LOGGER_FMT}" ) - FATAL - $action unknown Loglevel" | tee -a "${LOG_FILE}" 1>&2 ;; + esac true; }