-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 # | ||
####################################################################### | ||
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
|
||
else | ||
echo "$msg" | ||
local levelColor=$(_getLevelColor $level) | ||
echo -e "${levelColor}${msg}${BASH_COLOR_CODE_DEFAULT}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
fi | ||
} | ||
|
||
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the trailing space. |
||
} | ||
|
||
function _CTX() { | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
fi | ||
} | ||
|
||
function _getLevelColor(){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put this function definition before There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function definition should be |
||
local level=$1 | ||
_levelExists $level | ||
local levelExists=$? | ||
|
||
if [[ "$levelExists" == "0" ]]; then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
echo ${ARRAY_LEVEL_COLOR["$level"]} | ||
else | ||
echo $BASH_COLOR_CODE_DEFAULT | ||
fi | ||
|
||
|
||
} | ||
####################################################################### | ||
# public methods # | ||
####################################################################### | ||
|
@@ -104,3 +147,8 @@ function WARN() { | |
function ERROR() { | ||
_log "$1" | ||
} | ||
|
||
function SUCCESS() { | ||
_log "$1" | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,24 @@ set -o pipefail | |
|
||
CUR_FILE_DIR="$(cd "$( dirname "$0" )" && pwd)" | ||
|
||
|
||
|
||
function loggerShCheck() | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
# 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 "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because of |
||
|
||
if [[ "$?" == 0 ]]; then | ||
echo "logger.sh does not have syntax error" | ||
fi | ||
} | ||
|
||
loggerShCheck | ||
|
||
|
||
|
||
|
||
source "$CUR_FILE_DIR"/../src/logger.sh "" | ||
|
||
function foo() { | ||
|
@@ -16,9 +34,41 @@ function foo() { | |
echo "echo message" | ||
WARN "WARN message" | ||
ERROR "ERROR message" | ||
SUCCESS "SUCCESS message" | ||
EXIT | ||
} | ||
|
||
|
||
|
||
|
||
function colorCheck() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is unnecessary. The |
||
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 | ||
|
There was a problem hiding this comment.
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.