Timestamps and logging with echo in bash

I'm often writing scripts to automate stuff, and having good logs of activity is really important. But, writing a logging API and using it in a bash script seems overkill and a real pain, especially as your script will then be full of calls to your custom functions. Why not rather extend bash echo, and use that?

So, stick this at the beginning of any script, and you can use 'echo' for simple but effective logging:

function echo {
  /bin/echo `date` $* | tee -a foo.log
}

From now on, every time you use something like "echo Fixing buffer...", it will print it to the screen with a timestamp, and also log it to the foo.log log file, with a timestamp.

Of course, variants could be to only log to the log file, to only show the timestamp in the log file, to.....

If you can do without the timestamp, you can force all standard output to a log file by putting this at the top of your script:

exec > mylog.log 2>&1

Presumably, if you want to see it to the console too, you could to:

exec 2>&1 | tee foo.log