Skip to content

Instantly share code, notes, and snippets.

@robustican
Created July 10, 2020 15:14
Show Gist options
  • Save robustican/2685f8961524dfd8dbf6ce44f0944a21 to your computer and use it in GitHub Desktop.
Save robustican/2685f8961524dfd8dbf6ce44f0944a21 to your computer and use it in GitHub Desktop.
export LOG_DIR=~/logs
# `tee` the stdout/stderr of a command to
# ${LOG_DIR}/<binary>/$(datetime).log or ${LOG_DIR}/<binary>/<subcommand>/$(datetime).log
# $1: full command (including arguments) to log
log_command() {
local log_dir="${LOG_DIR}/${1}"
if [ "$1" = "mortar" ]; then
log_dir="${log_dir}/$2"
fi
mkdir -p "${log_dir}"
echo "${log_dir}"
local log_filepath="${log_dir}/$(date '+%F-%T' | sed 's/:/-/g').log"
echo -e "${*}\n" > "${log_filepath}"
# combine STDERR and STDOUT
( time "${@}" 2>&1 ) | tee -a "${log_filepath}"
echo "${log_filepath}"
}
alias logc=log_command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment