Log when your scripts start and end

When you write scripts that are the controllers for processing data make sure to log when they start and finish the work. It is so frustrating to debug an erroneous processing result -- often days or weeks later -- and not have any indication when the process actually started and finished. A missing finished log entry is the first indication that the processing ended abruptly.

So, in bash shell script use
#!/bin/bash
. logger.sh
info starting
....
info finished
#END
where logger.sh is somewhere in your path and has the content
#!/bin/bash

# logger.sh
#
# info, warning, error and fatal functions to display timestamped
# messages to stderr. Use the infof, warningf, errorf and fatalf
# functions to format the message using printf. A newline is
# automatically added and should not be included in your printf
# format string.

function info {
log INFO $*
}

function infof {
logf INFO $*
}

function warning {
log WARNING $*
}

function warningf {
logf WARNING $*
}

function error {
log ERROR $*
}

function errorf {
logf ERROR $*
}

function fatal {
log FATAL $*
exit 1;
}

function fatalf {
logf FATAL $*
exit 1;
}

function log {
echo $(date +'%F %T') $* 1>&2
}

function logf {
level=$1
format=$2
shift 2
printf "$(date +'%F %T') $level $format\n" $* 1>&2
}

# END
Most other languages have extensive logging support.

Hardly seems worth posting such an obvious piece of advice, but look around into your scripts -- as I did mine -- and notice how few provide this data.

No comments: