# # LECA Bash library # # The LECA bash library provides a set of function used for helping # development of bash script mainly to write job script on the luke # cluster # # The files from the LECA Bash library must be sourced from your main script # # # Logging facilities for bash # =========================== # # Provides the following functions: # # openlogfile # Redirected all logging to the file specified by FILENAME. # If the file already exists, new logs are appened at the end of the file. # If the file does not exist it is created # # closelogfile # Closes the current logfile and redirect the logging to stderr. # # logdebug # Writes message as a debug level log to the current log file. # # loginfo # Writes message as an info level log to the current log file. # # logwarning # Writes message as a warning level log to the current log file. # # logerror # Writes message as an error level log to the current log file. # # setloglevel # Set the current logging level to the level specified by LEVEL. # Level is DEBUG, INFO, WARNING or ERROR. # A log is recorded only when it is submitted with a level # greater or equal to the current logging level. # # All the log will be done on file descriptor 3 # By default file descriptor 3 is redirected to stderr (2) # By default the logging level is set to WARNING. # Therefore only WARNING and ERROR messages will be actually logged. LOG_DEBUG_LEVEL=1 LOG_INFO_LEVEL=2 LOG_WARNING_LEVEL=3 LOG_ERROR_LEVEL=4 LOG_LEVEL=2 exec 3>&2 ##### # # The logging function function logdebug() { if (( LOG_LEVEL <= LOG_DEBUG_LEVEL )); then echo `date +'%Y-%m-%d %H:%M:%S'` "[DEBUG ] $(hostname).$$ -- $*" 1>&3 fi } function loginfo() { if (( LOG_LEVEL <= LOG_INFO_LEVEL )); then echo `date +'%Y-%m-%d %H:%M:%S'` "[INFO ] $(hostname).$$ -- $*" 1>&3 fi } function logerror() { if (( LOG_LEVEL <= LOG_ERROR_LEVEL )); then echo `date +'%Y-%m-%d %H:%M:%S'` "[ERROR ] $(hostname).$$ -- $*" 1>&3 fi } function logwarning() { if (( LOG_LEVEL <= LOG_WARNING_LEVEL )); then echo `date +'%Y-%m-%d %H:%M:%S'` "[WARNING] $(hostname).$$ -- $*" 1>&3 fi } function setloglevel () { LOG_LEVEL=$(indirect "LOG_${1}_LEVEL") loginfo "Logging level set to : ${1} ($LOG_LEVEL)" } function openlogfile() { exec 3>> "$1" LOGFILE="$1" } function closelogfile() { if [[ ! -z "${LOGFILE}" ]]; then exec 3>&- exec 3>&2 LOGFILE="" fi } logdebug "Load logging package"