Last active
May 28, 2022 06:21
-
-
Save jdmedeiros/643760e235272e43af9fac0afeb78b2f to your computer and use it in GitHub Desktop.
Efficient Logging Mechanism in Shell ( log4sh ) [https://www.cubicrace.com/2016/03/efficient-logging-mechnism-in-shell.html]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
SCRIPT_LOG=logfile.log | |
# https://serverfault.com/questions/103501/how-can-i-fully-log-all-bash-scripts-actions | |
exec 3>&1 4>&2 | |
trap 'exec 2>&4 1>&3' 0 1 2 3 | |
exec 1>$SCRIPT_LOG 2>&1 | |
source ./logger.sh | |
SCRIPTENTRY | |
updateUserDetails(){ | |
ENTRY | |
DEBUG "Username: $1, Key: $2" | |
INFO "User details updated for $1" | |
EXIT | |
} | |
INFO "Updating user details..." | |
updateUserDetails "cubicrace" "3445" | |
rc=2 | |
if [ ! "$rc" = "0" ] | |
then | |
ERROR "Failed to update user details. RC=$rc" | |
fi | |
SCRIPTEXIT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
touch $SCRIPT_LOG | |
function SCRIPTENTRY(){ | |
timeAndDate=`date` | |
script_name=`basename "$0"` | |
script_name="${script_name%.*}" | |
echo "[$timeAndDate] [DEBUG] > $script_name $FUNCNAME" >> $SCRIPT_LOG | |
} | |
function SCRIPTEXIT(){ | |
script_name=`basename "$0"` | |
script_name="${script_name%.*}" | |
echo "[$timeAndDate] [DEBUG] < $script_name $FUNCNAME" >> $SCRIPT_LOG | |
} | |
function ENTRY(){ | |
local cfn="${FUNCNAME[1]}" | |
timeAndDate=`date` | |
echo "[$timeAndDate] [DEBUG] > $cfn $FUNCNAME" >> $SCRIPT_LOG | |
} | |
function EXIT(){ | |
local cfn="${FUNCNAME[1]}" | |
timeAndDate=`date` | |
echo "[$timeAndDate] [DEBUG] < $cfn $FUNCNAME" >> $SCRIPT_LOG | |
} | |
function INFO(){ | |
local function_name="${FUNCNAME[1]}" | |
local msg="$1" | |
timeAndDate=`date` | |
echo "[$timeAndDate] [INFO] $msg" >> $SCRIPT_LOG | |
} | |
function DEBUG(){ | |
local function_name="${FUNCNAME[1]}" | |
local msg="$1" | |
timeAndDate=`date` | |
echo "[$timeAndDate] [DEBUG] $msg" >> $SCRIPT_LOG | |
} | |
function ERROR(){ | |
local function_name="${FUNCNAME[1]}" | |
local msg="$1" | |
timeAndDate=`date` | |
echo "[$timeAndDate] [ERROR] $msg" >> $SCRIPT_LOG | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment