Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some small additions #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*~
log.txt
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions demo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
26 changes: 21 additions & 5 deletions tinylogger.bash
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,33 @@
# THE SOFTWARE.
#
# Author: Nagarjuna Kumarappan <[email protected]>
#
# 15.04.2021
#
# Changes:
# - Log Level fatal added
# - Log output redirected to log file
# - shellcheck recommendations implemented
# - Default catch added
#
# Author: Markus Heene <[email protected]>

# 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; }