Last active
January 25, 2017 00:41
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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