Skip to content

Commit

Permalink
Import the 'Add BASHLOG_EXIT_ON_ERROR boolean and associated tests' p…
Browse files Browse the repository at this point in the history
…ull request from upstream (Zordrak#1) & fix compatibility issue with /bin/sh (#1)
  • Loading branch information
Lionel VICTOR committed May 26, 2022
2 parents 74833c9 + caf1f7a commit 5a2a3a8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
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
9 changes: 6 additions & 3 deletions log.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ function log() {
local date_s="$(date "+%s")";

local file="${BASHLOG_FILE:-0}";
local file_path="${BASHLOG_FILE_PATH:-/tmp/$(basename "${0}").log}";
local file_path="${BASHLOG_FILE_PATH:-/tmp/$(basename -- "${0}").log}";

local json="${BASHLOG_JSON:-0}";
local json_path="${BASHLOG_JSON_PATH:-/tmp/$(basename "${0}").log.json}";
local json_path="${BASHLOG_JSON_PATH:-/tmp/$(basename -- "${0}").log.json}";

local syslog="${BASHLOG_SYSLOG:-0}";
local tag="${BASHLOG_SYSLOG_TAG:-$(basename "${0}")}";
local tag="${BASHLOG_SYSLOG_TAG:-$(basename -- "${0}")}";
local facility="${BASHLOG_SYSLOG_FACILITY:-local0}";
local pid="${$}";

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

0 comments on commit 5a2a3a8

Please sign in to comment.