Last active
July 31, 2017 14:46
-
-
Save Bost/54291d824149f0c4157b40329fceb02c to your computer and use it in GitHub Desktop.
bash: logging a la log4j
This file contains 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
#!/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