Skip to content

Instantly share code, notes, and snippets.

@Bost
Last active July 31, 2017 14:46
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 Bost/54291d824149f0c4157b40329fceb02c to your computer and use it in GitHub Desktop.
Save Bost/54291d824149f0c4157b40329fceb02c to your computer and use it in GitHub Desktop.
bash: logging a la log4j
#!/usr/bin/env bash
# Debugging
# set -x
# Stop on error
set -e
# Exit if variables are not set (recommended)
set –u
logfile=file.log
tstp() {
date +"%Y-%m-%d %H:%M:%S,%3N"
}
# TODO save the output of $@ to a the ${logfile}
exeinf() {
echo "INFO " $(tstp) "$ "$@ | tee --append ${logfile}
$@
}
exeerr() {
echo "ERROR" $(tstp) "$ "$@ 2>&1 | tee --append ${logfile}
$@
}
exeinf "Executing bash command: ${0} $@"
exeerr "Executing bash command: ${0} $@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment