From 228b32f81a36d4c3d69a5f5ea8b283501dbde959 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sun, 21 May 2023 15:20:15 +0200 Subject: [PATCH] add logger level --- script | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/script b/script index c3c5f8a..cf486f7 100755 --- a/script +++ b/script @@ -3,7 +3,7 @@ set -eu usage() { - printf "Usage: %s [-h]\n" "$0" + printf "Usage: %s [-l debug|info|error] [-h]\n" "$0" } help() { @@ -18,6 +18,9 @@ help() { OPTIONS -h Show this help + + -l debug|info|error + Debug level EOH } @@ -28,17 +31,15 @@ on_interrupt() { } main() { - while getopts "hf:" option; do + while getopts "l:h" option; do case "${option}" in h) help; exit 0;; - f) FOO="$OPTARG";; + l) LOG_VERBOSE="$OPTARG";; ?) log -l error "$(usage)"; exit 1;; esac done - FOO="${FOO:-defaultValue}" - - # log [-t] [-l debug|error] message + # log [-t] [-l debug|info|notice|warning|error] message exit 0 } @@ -57,13 +58,27 @@ log() { if [ -t 2 ] && [ -z "${NO_COLOR-}" ]; then case "${LEVEL}" in - debug) COLOR="$(tput setaf 3)";; - error) COLOR="$(tput setaf 1)";; - *) COLOR="$(tput sgr0)" + debug) COLOR="$(tput setaf 3)"; LEVEL=100;; + notice) COLOR="$(tput setaf 4)"; LEVEL=250;; + warning) COLOR="$(tput setaf 5)"; LEVEL=300;; + error) COLOR="$(tput setaf 1)"; LEVEL=400;; + *) COLOR="$(tput sgr0)"; LEVEL=200;; esac fi - printf "%s%s%s\n" "${COLOR:-}" "${TIME:-}" "$*" >&2 + LOG_VERBOSE="${LOG_VERBOSE:-info}" + + case "${LOG_VERBOSE}" in + debug) LOG_VERBOSE_VALUE=100;; + notice) LOG_VERBOSE_VALUE=250;; + warning) LOG_VERBOSE_VALUE=300;; + error) LOG_VERBOSE_VALUE=400;; + *) LOG_VERBOSE_VALUE=200;; + esac + + if [ $LEVEL -ge $LOG_VERBOSE_VALUE ]; then + printf "%s%s%s\n" "${COLOR:-}" "${TIME:-}" "$*" >&2 + fi } trap on_interrupt INT