WeberCode Bash Logging

Created by Troy Weber last modified
#!/usr/bin/env bash

# you can source or run this file
# run as a script, it will print the string using your arguments with the format function

# Usage: log.sh FORMAT_NAME FORE_COLOR BACK_COLOR [TEXT_ARG]...
# Example: log.sh BOLD GREEN WHITE hello world!

# bash codes from http://misc.flogisoft.com/bash/tip_colors_and_formatting

# FORMAT_CODE
format_DEFAULT=
format_BOLD=1
format_DIM=2
format_UNDERLINE=4
format_BLINK=5
format_INVERT=7
format_HIDDEN=8

# COLOR_CODE (these are foreground, for background we add 10)
format_color_DEFAULT=39
format_color_BLACK=30
format_color_RED=31
format_color_GREEN=32
format_color_YELLOW=33
format_color_BLUE=34
format_color_MAGENTA=35
format_color_CYAN=36
format_color_LIGHT_GRAY=37
format_color_DARK_GRAY=90
format_color_LIGHT_RED=91
format_color_LIGHT_GREEN=92
format_color_LIGHT_YELLOW=93
format_color_LIGHT_BLUE=94
format_color_LIGHT_MAGENTA=95
format_color_LIGHT_CYAN=96
format_color_WHITE=97

# Usage: format_accent FORMAT_NAME [TEXT_ARG]...
# Example: echo -e `format_accent BOLD hello world!`
function format_accent()
{
	eval "S=\${format_$1}"
	if [[ "$1" == "DEFAULT" ]]
	then
		E=$S
	else
		E="$(($S+20))"
	fi
	echo "\e[${S}m${@:2}\e[${E}m"
}

# Usage: format_fore COLOR_NAME [TEXT_ARG]...
# Example: echo -e `format_fore GREEN hello world!`
function format_fore()
{
	eval "S=\${format_color_$1}"
	E="$format_color_DEFAULT"
	echo "\e[${S}m${@:2}\e[${E}m"
}

# Usage: format_back COLOR_NAME [TEXT_ARG]...
# Example: echo -e `format_back GREEN hello world!`
function format_back()
{
	eval "S=\$((\${format_color_$1}+10))"
	E="$(($format_color_DEFAULT+10))"
	echo "\e[${S}m${@:2}\e[${E}m"
}

# Usage: format_colors FORE_COLOR BACK_COLOR [TEXT_ARG]...
# Example: echo -e `format_colors GREEN WHITE hello world!`
function format_colors()
{
	echo $(format_fore $1 $(format_back $2 ${@:3}))
}

# Usage: format FORMAT_NAME FORE_COLOR BACK_COLOR [TEXT_ARG]...
# Example: echo -e `format BOLD GREEN WHITE hello world!`
function format()
{
	echo $(format_accent $1 $(format_colors $2 $3 ${@:4}))
}

if [[ "$0" == "$BASH_SOURCE" ]]
then
	case $1 in
		-h)
			SCRIPT=`basename $0`
			echo -e "Usage:   $(format_accent BOLD ${SCRIPT} FORMAT_NAME FORE_COLOR BACK_COLOR [TEXT_ARG]...)"
			echo
			echo -e "Example: $(format_accent BOLD ${SCRIPT} BOLD GREEN DARK_GRAY hello world!)"
			echo -e "Result:  $(format BOLD GREEN DARK_GRAY hello world!)"
			;;
		*)
			echo -e `format $1 $2 $3 ${@:4}`
			;;
	esac
else
	if [[ "$1" == "-v" ]]
	then
		echo "formatting sourced!"
	fi
fi
#!/usr/bin/env bash

source <(curl -s https://bitbucket.org/api/2.0/snippets/webercode/dEjpyx/master/files/format.sh)

regex="^-?[0-9]+$"
if [[ $1 =~ $regex ]]
then
	log_LEVEL=$1
else
	case $1 in
		[eE][rR][rR][oO][rR]) log_LEVEL=1;;
		[wW][aA][rR][nN]) log_LEVEL=2;;
		[iI][nN][fF][oO]) log_LEVEL=3;;
		[dD][eE][bB][uU][gG]) log_LEVEL=4;;
		[tT][rR][aA][cC][eE]) log_LEVEL=5;;
		*) log_LEVEL=3;;
	esac
fi

function log_date() { echo "[$(format_fore $1 $(date +"%F %T"))]"; }
function log_label() { echo "[$(format_fore $1 $2)]"; }
function log_error() { if [[ ${log_LEVEL} -ge 1 ]]; then echo -e "$(log_date DARK_GRAY)$(log_label LIGHT_RED ERROR) $@"; fi; }
function log_warn() { if [[ ${log_LEVEL} -ge 2 ]]; then echo -e "$(log_date DARK_GRAY)$(log_label LIGHT_YELLOW WARN) $@"; fi; }
function log_info() { if [[ ${log_LEVEL} -ge 3 ]]; then echo -e "$(log_date DARK_GRAY)$(log_label LIGHT_GREEN INFO) $@"; fi; }
function log_debug() { if [[ ${log_LEVEL} -ge 4 ]]; then echo -e "$(log_date DARK_GRAY)$(log_label LIGHT_BLUE DEBUG) $@"; fi; }
function log_trace() { if [[ ${log_LEVEL} -ge 5 ]]; then echo -e "$(log_date DARK_GRAY)$(log_label LIGHT_MAGENTA TRACE) $@"; fi; }

Comments (1)

  1. Troy Weber

    To easily include these any of these bash functions into your script, you can source the script with:

    # source the formats
    source <(curl -s https://bitbucket.org/api/2.0/snippets/webercode/dEjpyx/master/files/format.sh)
    # source the log functions (the previous sourcing should occur on its own): the levels are error, warn, info, debug, trace or a number, defaults to info level
    source <(curl -s https://bitbucket.org/api/2.0/snippets/webercode/dEjpyx/master/files/log.sh) trace