From 416410fc6ddb0d9a0e10b89b009c4758bb1fabc2 Mon Sep 17 00:00:00 2001 From: Bradley Cicenas Date: Sun, 12 Nov 2017 13:32:52 +0000 Subject: [PATCH 1/3] add level-based color output --- tinylogger.bash | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/tinylogger.bash b/tinylogger.bash index ae36cc8..d2abe3c 100644 --- a/tinylogger.bash +++ b/tinylogger.bash @@ -24,15 +24,31 @@ # Author: Nagarjuna Kumarappan # defaults -LOGGER_FMT=${LOGGER_FMT:="%Y-%m-%d %H:%M:%S"} LOGGER_LVL=${LOGGER_LVL:="info"} +LOGGER_FMT=${LOGGER_FMT:="%Y-%m-%d %H:%M:%S"} +LOGGER_COLOR=${LOGGER_COLOR:="1"} + +_tlog_levels=(error warn info debug) +_tlog_colors=('\033[0;31m' '\033[0;33m' '\033[0;32m' '\033[0;36m') + +function _print_lvl { + idx=$1 + nc='\033[0m' + lvlname=${_tlog_levels[$idx]^^} + + if (("$LOGGER_COLOR")); then + printf "${_tlog_colors[$idx]}${lvlname}${nc}" + else + printf "${lvlname}" + fi +} function tlog { 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}" ) - $(_print_lvl 3) - $@" 1>&2 ;; + info) [[ $LOGGER_LVL =~ debug|info ]] && echo "$( date "+${LOGGER_FMT}" ) - $(_print_lvl 2) - $@" 1>&2 ;; + warn) [[ $LOGGER_LVL =~ debug|info|warn ]] && echo "$( date "+${LOGGER_FMT}" ) - $(_print_lvl 1) - $@" 1>&2 ;; + error) [[ ! $LOGGER_LVL =~ none ]] && echo "$( date "+${LOGGER_FMT}" ) - $(_print_lvl 0) - $@" 1>&2 ;; esac true; } From 263a7156d193d7d9d7c0df54f3123e5a99f5f7e7 Mon Sep 17 00:00:00 2001 From: Bradley Cicenas Date: Sun, 12 Nov 2017 13:36:15 +0000 Subject: [PATCH 2/3] use common line prefix function --- tinylogger.bash | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/tinylogger.bash b/tinylogger.bash index d2abe3c..f31e618 100644 --- a/tinylogger.bash +++ b/tinylogger.bash @@ -31,24 +31,25 @@ LOGGER_COLOR=${LOGGER_COLOR:="1"} _tlog_levels=(error warn info debug) _tlog_colors=('\033[0;31m' '\033[0;33m' '\033[0;32m' '\033[0;36m') -function _print_lvl { +function _tlog_head { idx=$1 nc='\033[0m' lvlname=${_tlog_levels[$idx]^^} if (("$LOGGER_COLOR")); then - printf "${_tlog_colors[$idx]}${lvlname}${nc}" + printf "$(date "+${LOGGER_FMT}") - ${_tlog_colors[$idx]}${lvlname}${nc}" else - printf "${lvlname}" + printf "$(date "+${LOGGER_FMT}") - ${lvlname}" fi } function tlog { - action=$1 && shift - case $action in - debug) [[ $LOGGER_LVL =~ debug ]] && echo "$( date "+${LOGGER_FMT}" ) - $(_print_lvl 3) - $@" 1>&2 ;; - info) [[ $LOGGER_LVL =~ debug|info ]] && echo "$( date "+${LOGGER_FMT}" ) - $(_print_lvl 2) - $@" 1>&2 ;; - warn) [[ $LOGGER_LVL =~ debug|info|warn ]] && echo "$( date "+${LOGGER_FMT}" ) - $(_print_lvl 1) - $@" 1>&2 ;; - error) [[ ! $LOGGER_LVL =~ none ]] && echo "$( date "+${LOGGER_FMT}" ) - $(_print_lvl 0) - $@" 1>&2 ;; - esac - true; } + action=$1 && shift + case $action in + debug) [[ $LOGGER_LVL =~ debug ]] && echo "$(_tlog_head 3) - $@" 1>&2 ;; + info) [[ $LOGGER_LVL =~ debug|info ]] && echo "$(_tlog_head 2) - $@" 1>&2 ;; + warn) [[ $LOGGER_LVL =~ debug|info|warn ]] && echo "$(_tlog_head 1) - $@" 1>&2 ;; + error) [[ ! $LOGGER_LVL =~ none ]] && echo "$(_tlog_head 0) - $@" 1>&2 ;; + esac + true; +} From fbd14888322e905d99a873b206f5721c3321cdae Mon Sep 17 00:00:00 2001 From: Bradley Cicenas Date: Mon, 11 Dec 2017 01:13:40 +0000 Subject: [PATCH 3/3] add color section to README --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index b77ec26..7c56f2f 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,9 @@ You can control the logging level while calling a script by setting the `LOGGER_ You can set `LOGGER_LVL=none` to disable all logging. +## Color +By default, log output is colorized by level. This behavior can be disabled by setting `LOGGER_COLOR=0` + ## Timestamp customisation Underneath, the `date` command is used to display the timestamp. Simply set the `LOGGER_FMT` to any valid format supported by `date`.