add logger level
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Simon Vieille 2023-05-21 15:20:15 +02:00
parent 5fb88af95c
commit 228b32f81a
Signed by: deblan
GPG key ID: 579388D585F70417

35
script
View file

@ -3,7 +3,7 @@
set -eu set -eu
usage() { usage() {
printf "Usage: %s [-h]\n" "$0" printf "Usage: %s [-l debug|info|error] [-h]\n" "$0"
} }
help() { help() {
@ -18,6 +18,9 @@ help() {
OPTIONS OPTIONS
-h Show this help -h Show this help
-l debug|info|error
Debug level
EOH EOH
} }
@ -28,17 +31,15 @@ on_interrupt() {
} }
main() { main() {
while getopts "hf:" option; do while getopts "l:h" option; do
case "${option}" in case "${option}" in
h) help; exit 0;; h) help; exit 0;;
f) FOO="$OPTARG";; l) LOG_VERBOSE="$OPTARG";;
?) log -l error "$(usage)"; exit 1;; ?) log -l error "$(usage)"; exit 1;;
esac esac
done done
FOO="${FOO:-defaultValue}" # log [-t] [-l debug|info|notice|warning|error] message
# log [-t] [-l debug|error] message
exit 0 exit 0
} }
@ -57,13 +58,27 @@ log() {
if [ -t 2 ] && [ -z "${NO_COLOR-}" ]; then if [ -t 2 ] && [ -z "${NO_COLOR-}" ]; then
case "${LEVEL}" in case "${LEVEL}" in
debug) COLOR="$(tput setaf 3)";; debug) COLOR="$(tput setaf 3)"; LEVEL=100;;
error) COLOR="$(tput setaf 1)";; notice) COLOR="$(tput setaf 4)"; LEVEL=250;;
*) COLOR="$(tput sgr0)" warning) COLOR="$(tput setaf 5)"; LEVEL=300;;
error) COLOR="$(tput setaf 1)"; LEVEL=400;;
*) COLOR="$(tput sgr0)"; LEVEL=200;;
esac esac
fi 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 trap on_interrupt INT