# socialnerds log engine for bash/shell scripts # # version: 0.1a # author: david@socialnerds.org # **** help **** # this is basically a collection of bash functions # for making a decent logging in a bash script # amazingly easy. # # in order to use this little logging script # you need to have some global variables defined # loglevel=<0-4> # 0 -- no logging # 1 -- just errors # 2 -- errors & warnings # 3 -- even infos (something like "user was created") # 4 -- debug (more than everything) # # logfile="/path/to/logfile.log" # well.. should be understandable.. # # logwhat="kvm-tools" # tells the engine what is actually been logged (example: name of calling script) # just used for syslog # # log2syslog="0|1" # (de)activates logging to the syslog deamen # # log2stdout="0|1" # (de)activates logging to stdout # useful if you don't want to deal with output in your script # # log2file="0|1" # (de)activates logging to logfile # # # usage: # log error|warning|info|debug "log message" # **** log function **** log() { # getting local hostname local hostname=$(cat /etc/hostname) # if no loglevel is defined the script will end if [ -z $loglevel ]; then echo "logengine[error]: no loglevel defined" exit 1 # if loglevel is 0, skip logging elif [ $loglevel = "0" ]; then # logging is disabled return 0 # loglevel has to be something between 0 and 5 elif [ ! $loglevel -gt "0" -o ! $loglevel -lt "5" ]; then # loglevel unknown echo "logengine[error]: unknown loglevel" exit 1 fi # stdout routine if [ -z $log2stdout ]; then log2stdout="0" elif [ $log2stdout = "0" ]; then # stdout is disabled : elif [ $log2stdout = "1" ]; then # here goes the actual logging process (stdout) if [ $1 = "error" -a $loglevel -gt "0" ]; then # errors echo "[$1]: $2" elif [ $1 = "warning" -a $loglevel -gt "1" ]; then # warnings echo "[$1]: $2" elif [ $1 = "info" -a $loglevel -gt "2" ]; then # informational echo "[$1]: $2" elif [ $1 = "debug" -a $loglevel -gt "3" ]; then # debug messages echo "[$1]: $2" fi fi # syslog routine if [ -z $log2syslog ]; then log2syslog="0" elif [ $log2syslog = "0" ]; then # syslog is disabled : elif [ -z $logwhat ]; then # for syslogging logwhat has to be defined echo "logengine[error]: logwhat not defined" elif [ $log2syslog = "1" ]; then # here goes the actual logging process (syslog) if [ $1 = "error" -a $loglevel -gt "0" ]; then # errors logger -t "$logwhat[$1]" "$2" elif [ $1 = "warning" -a $loglevel -gt "1" ]; then # warnings logger -t "$logwhat[$1]" "$2" elif [ $1 = "info" -a $loglevel -gt "2" ]; then # informational logger -t "$logwhat[$1]" "$2" elif [ $1 = "debug" -a $loglevel -gt "3" ]; then # debug messages logger -t "$logwhat[$1]" "$2" fi fi # logfile routine if [ -z $log2file ]; then log2file="0" elif [ $log2file = "0" ]; then # file is disabled : elif [ $log2file = "1" ]; then if [ -z $logfile ]; then # if no logfile is defined the script will end echo "logengine[error]: no logfile defined" exit 1 elif ! [ -a $logfile ]; then echo "logengine[warning]: logfile not found" echo "logengine[info]: creating new logfile" touch $logfile if [ $? != 0 ]; then # probably a permission denied error echo "logengine[error]: could not create logfile." exit 1 fi fi # here goes the actual logging process (logfile) if [ $1 = "error" -a $loglevel -gt "0" ]; then # errors echo "$(gettimestamp nice) [$1]: $2" >> $logfile elif [ $1 = "warning" -a $loglevel -gt "1" ]; then # warnings echo "$(gettimestamp nice) [$1]: $2" >> $logfile elif [ $1 = "info" -a $loglevel -gt "2" ]; then # informational echo "$(gettimestamp nice) [$1]: $2" >> $logfile elif [ $1 = "debug" -a $loglevel -gt "3" ]; then # debug messages echo "$(gettimestamp nice) [$1]: $2" >> $logfile fi fi } # end of file