Bash – redirect and log script output

bashexeclogsshell-script

I am trying to tidy up the following snippets, design goals are to log all output from a script, and should not be a wrapper. Less lines are better.

I don't care about user inputs (at this stage), target scripts are run non-interactively.

The snippet needs to

  • output stdout to log, and always echo to console
  • output stderr to log, and echo to console iff debugging is enabled
  • stderr messages should be prefixed with time stamps and other usefulness

At the moment I have the following which only tests out under recent versions of bash (4.2+?) like in Ubuntu precise, but misbehaves on CentOS6.

DEBUG_LOG="${0##*/}.log"

# copy stdout to log always and echo to console
exec >  >(tee -a ${DEBUG_LOG})           

# copy stderr to log only, unless debugging is enabled
[ $DEBUG_TEST = "true" ] \
  && exec 2> >(tee -a ${DEBUG_LOG} >&2) \
  || exec 2>> ${DEBUG_LOG}

Then this…

# Expand escaped characters, wrap at 70 chars on spaces, 
# and indent wrapped lines
msg_log() { 
  echo -e "$(date +%T) ${0##*/}: $1" \
    | fold -w70 -s | sed '2~1s/^/  /' >&2; 
}
msg_con() { 
  if [ "${DEBUG_TEST}" = "true" ]; then 
    msg_log "$1"
  else
    echo -e "$1" | fold -w70 -s | sed '2~1s/^/  /'; 
  fi
}

Instead of echo I can call one of those msg procedures, e.g., msg_con "hello world".
Also script output will then go to stderr by setting as an environment variable at call time, e.g., DEBUG_TEST=true myscript.

I have read that exec may not work in some shells such as busybox. There is a mkfifo and fork combination at https://stackoverflow.com/a/5200754 that does something similar but I'd rather not use fork unless absolutely needed.

Prefer bash examples please, but something that works under sh or is more portable would be nice. Any ideas?

Best Answer

function startLogging {
    exec > >(gawk -v pid=$$ '{ print strftime("%F-%T"),pid,$0; fflush(); }' | tee -a $logfile)
    [ ! -z "$DEBUG" ] && exec 2>&1 || exec 2> >(gawk -v pid=$$ '{ print strftime("%F-%T"),pid,$0; fflush(); }' >>$logfile)
    echo "=== Log started for $$ at $(date +%F-%T) ==="
}

You need to have $logfile set to something

Related Question