Skip to content

Conversation

@Miatec
Copy link

@Miatec Miatec commented Jan 4, 2020

I've unnexpectedly found the time to develop the feature I proposed you (issue #1 ).

3 files where modified:

  • src/logger.sh - obviously to add the feature
  • test/logger.sh - to test the feature and add a test on the syntax validity of src/logger.sh
  • readme.md - to add the description of the new feature.

Copy link
Owner

@adoyle-h adoyle-h left a comment

Choose a reason for hiding this comment

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

Thanks for your commits. I have reviewed it and left some comments.

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.

_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.

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
}

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(){.

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.

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"

{
# 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.



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.

*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.




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.

@adoyle-h
Copy link
Owner

adoyle-h commented Jan 5, 2020

image
Please sign the DCO.

@adoyle-h adoyle-h changed the title Implementing issue #1 "Colors on terminal". Support colored output on screen && add new level: SUCCESS Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants