Skip to content

Instantly share code, notes, and snippets.

@Aeon
Last active January 25, 2017 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aeon/0987618b84d624bc0da9cd743d18825b to your computer and use it in GitHub Desktop.
Save Aeon/0987618b84d624bc0da9cd743d18825b to your computer and use it in GitHub Desktop.
bash log to file with timestamp and log type prefix - based on http://serverfault.com/a/310104/15215
log() {
while IFS= read -r line; do
# prepend timestamp, first argument/second argument, and then input line
echo "[$(date -u +'%Y-%m-%d %H:%M:%S,%3N') $1/$2] $line"
done
}
#!/bin/bash
# load the log function definition
. log.sh
# log everything output from this script to logfile with prefixes
exec > >(log INFO $0 >> script.log)
exec 2> >(log ERROR $0 >> script.log)
echo "HUZZAH!"
cat /file/does/not/exist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment