Skip to content

Support colored output on screen && add new level: SUCCESS #2

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 1 commit 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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ bpkg install adoyle/shell-logger

## Usage

Only print message to `stdout`: `source logger.sh ""`.
Only print message to `stdout`: `source logger.sh ""`.
*Note: It will print lines in color according the level (e.g. ERROR in red, WARN in orange and so on)*

Print message to `stdout` and write it to a file: `source logger.sh "<output-path>"`.
*Note: No color of line in this mode*
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should support colored output to screen, while no-colored output to file.



## API

Expand Down
54 changes: 51 additions & 3 deletions src/logger.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@
# - http://www.cubicrace.com/2016/03/efficient-logging-mechnism-in-shell.html


#######################################################################
# Constants #
#######################################################################
BASH_COLOR_CODE_RED="\e[31m"
BASH_COLOR_CODE_YELLOW="\e[33m"
BASH_COLOR_CODE_GREEN="\e[32m"
BASH_COLOR_CODE_BLUE="\e[34m"
BASH_COLOR_CODE_DEFAULT="\e[39m"

declare -A ARRAY_LEVEL_COLOR=( \
[ENTER]=$BASH_COLOR_CODE_DEFAULT \
[EXIT]=$BASH_COLOR_CODE_DEFAULT \
[DEBUG]=$BASH_COLOR_CODE_DEFAULT \
[INFO]=$BASH_COLOR_CODE_BLUE \
[WARN]=$BASH_COLOR_CODE_YELLOW \
[ERROR]=$BASH_COLOR_CODE_RED \
[SUCCESS]=$BASH_COLOR_CODE_GREEN \
)

#######################################################################
# initialization #
#######################################################################
Expand All @@ -30,11 +49,14 @@ fi
#######################################################################

function _echo() {
local msg=$1
local msg=$2
local level=$1

if [[ -n "$LOG_TARGET" ]] ;then
echo "$msg" | tee >> "$LOG_TARGET"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should support colored output to screen, while no-colored output to file.

printf '%b%s%b\n' "${levelColor}" "${msg}" "${BASH_COLOR_CODE_DEFAULT}"
echo "$msg" >> "$LOG_TARGET"

else
echo "$msg"
local levelColor=$(_getLevelColor $level)
echo -e "${levelColor}${msg}${BASH_COLOR_CODE_DEFAULT}"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

msg may contain ANSI escape sequences.
Use printf '%b%s%b\n' "${levelColor}" "${msg}" "${BASH_COLOR_CODE_DEFAULT}".

fi
}

Expand All @@ -52,7 +74,7 @@ function _log() {
level="${2-${FUNCNAME[1]}}"
date_time=$(_date_time)
function_name="${FUNCNAME[2]}"
_echo "[$date_time][$level]($function_name) $msg"
_echo $level "[$date_time][$level]($function_name) $msg"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the trailing space.

}

function _CTX() {
Expand All @@ -72,7 +94,28 @@ function _CTX() {
echo "${ctx[@]}"
}

function _levelExists(){
local level=$1
if [[ "${ARRAY_LEVEL_COLOR[$level]+isset}" ]]; then
return 0
else
return 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use "echo yes/no" instead of "return 0/1" to represent the exit status of function. Because the process will be terminated with non-zero exit code when current shell has set set -o errexit.

fi
}

function _getLevelColor(){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this function definition before _echo.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function definition should be function name() {, not function name(){.

local level=$1
_levelExists $level
local levelExists=$?

if [[ "$levelExists" == "0" ]]; then
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_levelExists function is unnecessary.
I think if [[ "${ARRAY_LEVEL_COLOR[$level]:+isset}" ]]; then is simple enough.

echo ${ARRAY_LEVEL_COLOR["$level"]}
else
echo $BASH_COLOR_CODE_DEFAULT
fi


}
#######################################################################
# public methods #
#######################################################################
Expand Down Expand Up @@ -104,3 +147,8 @@ function WARN() {
function ERROR() {
_log "$1"
}

function SUCCESS() {
_log "$1"
}

50 changes: 50 additions & 0 deletions test/logger.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ set -o pipefail

CUR_FILE_DIR="$(cd "$( dirname "$0" )" && pwd)"



function loggerShCheck()
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use function loggerShCheck() {. Follow the coding style.

# bash -n check the syntax of logger.sh.
# If there is an error, it will stop the test script right now.
bash -n "$CUR_FILE_DIR"/../src/logger.sh ""
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of set -o errexit at line 3, the process will be terminated immediately when this line return non-zero exit code, and it will not reach line 18.


if [[ "$?" == 0 ]]; then
echo "logger.sh does not have syntax error"
fi
}

loggerShCheck




source "$CUR_FILE_DIR"/../src/logger.sh ""

function foo() {
Expand All @@ -16,9 +34,41 @@ function foo() {
echo "echo message"
WARN "WARN message"
ERROR "ERROR message"
SUCCESS "SUCCESS message"
EXIT
}




function colorCheck() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is unnecessary. The foo function will print colored texts.

local levelColor=$(_getLevelColor EXIT)
echo "EXIT has color code $levelColor"

levelColor=$(_getLevelColor ENTER)
echo "ENTER has color code $levelColor"

levelColor=$(_getLevelColor DEBUG)
echo "DEBUG has color code $levelColor"

levelColor=$(_getLevelColor INFO)
echo "INFO has color code $levelColor"

levelColor=$(_getLevelColor WARN)
echo "WARN has color code $levelColor"

levelColor=$(_getLevelColor ERROR)
echo "ERROR has color code $levelColor"

local levelColor=$(_getLevelColor "unexpectedLevel")
echo "unexpectedLevel has color code $levelColor"
}



colorCheck


ENTER

foo
Expand Down