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

Add BASHLOG_EXIT_ON_ERROR boolean and associated tests #1

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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ When BASHLOG_SYSLOG=1, logs are written to Syslog.
This determines the syslog tag to use, defaulting to the name of your script,
e.g. `yourscript.sh`

### BASHLOG_EXIT_ON_ERROR

Default: `0`

When BASHLOG_EXIT_ON_ERROR=1, `exit 1` will be called after logging an error.

This will allow your script to exit automatically after logging an error. This feature
is overriden when `DEBUG=1`.

## Recommended Usage

```
Expand Down
3 changes: 3 additions & 0 deletions log.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function log() {
local level="${1}";
local upper="$(echo "${level}" | awk '{print toupper($0)}')";
local debug_level="${DEBUG:-0}";
local exit_on_error="${BASHLOG_EXIT_ON_ERROR:-0}";

shift 1;

Expand Down Expand Up @@ -120,6 +121,8 @@ function log() {
if [ "${debug_level}" -gt 0 ]; then
echo -e "Here's a shell to debug with. 'exit 0' to continue. Other exit codes will abort - parent shell will terminate.";
bash || exit "${?}";
elif [ "${exit_on_error}" -gt 0 ]; then
exit 1;
fi;
;;
*)
Expand Down
27 changes: 27 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,17 @@ BASHLOG_SYSLOG=1;
DEBUG=0;

stderr="$(log 'error' "${random_string}" 2>&1 1>/dev/null)";
exit_code="${?}";
fileout="$(tail -n1 /tmp/${0}.log)";
jsonout="$(tail -n1 /tmp/${0}.log.json)";
syslogout="$(sudo tail -n1 /var/log/syslog)";

if [ "${exit_code}" -eq 0 ]; then
result ok 'error -> exit code 0';
else
result fail 'error -> exit code 0';
fi

grep -q -E $'^\033\[31m[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} \[ERROR\] '"${random_string}"$'\033\[0m$' <<<"${stderr}" \
&& result ok 'error -> stderr' \
|| result fail 'error -> stderr';
Expand Down Expand Up @@ -437,6 +444,26 @@ grep -q -E $'^./log.sh: line [0-9]+: /tmp/'"$(basename ${0})"'.log: Permission d
&& result ok 'error -> file, Permission denied -> stderr' \
|| result fail 'error -> file, Permission denied -> stderr';

##
# ERROR, EXIT ON ERROR ON
##

echo "Testing: 'error', BASHLOG_EXIT_ON_ERROR=1";

BASHLOG_FILE=0;
BASHLOG_JSON=0;
BASHLOG_SYSLOG=0;
BASHLOG_EXIT_ON_ERROR=1;
DEBUG=0;

stderr="$(log 'error' "${random_string}" 2>&1 1>/dev/null)";

if [ "${?}" -eq 1 ]; then
result ok 'error -> exit code 1';
else
result fail 'error -> exit code 1';
fi

##
# INTERACTIVE DEBUG
##
Expand Down