Last active
August 29, 2015 14:16
-
-
Save josuebrunel/03b4882428cad4913d20 to your computer and use it in GitHub Desktop.
Time added to log message
This file contains 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
################################################## | |
# | |
# Author : josue | |
# Filename : mylogger.sh | |
# Description : custom logger for my shell scripts | |
# Creation Date : 26-02-2015 | |
# Last Modified : Mon 09 Mar 2015 12:29:41 PM CDT | |
# | |
################################################## | |
## If a LOG_OUTPUT variable is defined, | |
## the logger will write to a file pointed to by the variable | |
function __log__(){ | |
level=$1 | |
message=$2 | |
output=$3 | |
if [ -z $output ]; then | |
if [ `uname -s` != "Darwin" ]; then | |
logger -s -i -t "[`date +'%Y-%m-%d %H:%M:%S'` ${HOSTNAME} ${USER}][`echo ${level} | tr '[:lower:]' '[:upper:]'`]" -p "user.${level}" "${message}" 2>&1 | |
else | |
logger -s -p "user.${level}" "${message}" 2>&1 | |
fi | |
else | |
if [ `uname -s` != "Darwin" ]; then | |
logger -s -i -t "[`date +'%Y-%m-%d %H:%M:%S'` ${HOSTNAME} ${USER}][`echo ${level} | tr '[:lower:]' '[:upper:]'`]" -p "user.${level}" "${message}" 2>> $output | |
else | |
logger -s -p "user.${level}" "${message}" 2>> $output | |
fi | |
fi | |
} | |
function __process__(){ | |
level=$1 | |
message=$2 | |
if [ ! "${message}" == "" ]; then | |
__log__ "${level}" "${message}" $LOG_OUTPUT | |
else | |
echo -e "A message must be provided i.e : _${level} 'your message'" | |
fi | |
} | |
function _info(){ | |
__process__ "info" "$1" | |
} | |
function _debug(){ | |
__process__ "debug" "$1" | |
} | |
function _warning(){ | |
__process__ "warning" "$1" | |
} | |
function _error(){ | |
__process__ "error" "$1" | |
} | |
function _notice(){ | |
__process__ "notice" "$1" | |
} | |
export -f __log__ | |
export -f __process__ | |
export -f _info | |
export -f _debug | |
export -f _warning | |
export -f _error | |
export -f _notice |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment