Tuesday, October 11, 2011

Función de Shell para loggear un mensaje de error, warning o debug

readonly LOGFILE="ems.log"

readonly ERROR=0
readonly WARNG=1
readonly DEBUG=2
readonly LOGTYPE=("ERROR" "WARNING" "DEBUG")

# Log messages to system log file (/var/log/message), the application log file and standard error if it is an error
# Example: Log $ERROR "$message" "$movie"
# Parameters:
# $1: Log type (1: ERROR, 2: WARNING, 3: DEBUG)
# $2: Message
# $3: Movie Name
Log() {
# If it is an error, send it to the standard error instead of the standard output
if [ $1 -eq $ERROR ]
then
exec 5>&1
exec 1>&2
fi

moviename=${3:-EMS}
[ ! -d "$TARGET_ENCODED_DIR/$moviename" ] && mkdir -p "$TARGET_ENCODED_DIR/$moviename"
echo "$(date +'[ %b %d %H:%M:%S ]') - ${LOGTYPE[${1:-2}]}: ${2:-Empty Message}" | tee -a "$TARGET_ENCODED_DIR/$moviename/$LOGFILE"
logger "${LOGTYPE[${1:-2}]} Encoding movie \"$3\": ${2:-Empty Message}"

# If was an error, rollback the standard output to the original value
if [ $1 -eq $ERROR ]
then
exec 1>&5
exec 5>&-
fi
}
Post a Comment