Skip to content

Instantly share code, notes, and snippets.

@antonagestam
Last active September 19, 2018 09:16
Show Gist options
  • Select an option

  • Save antonagestam/c528db4c8ff14e202bc56531f68f3948 to your computer and use it in GitHub Desktop.

Select an option

Save antonagestam/c528db4c8ff14e202bc56531f68f3948 to your computer and use it in GitHub Desktop.
helper functions for formatting log output
#!/usr/bin/env bash
# The logging functions all take input both as arguments and as stdin so that
# logging can be piped from other commands. The commands use this themselves:
# `log_sub` pipes into `log` which pipes into `prefix`.
prefix () {
local replace="s/^/$1/"
if (( $# > 1 )); then
echo "${@:2}" | sed "$replace"
else
tee | sed "$replace"
fi
}
log () {
local date_fmt='--iso-8601=seconds'
if (( $# > 0 )); then
echo "$*" | prefix "[$(date ${date_fmt})] "
else
tee | prefix "[$(date ${date_fmt})] "
fi
}
log_sub () {
local p=" ---> "
if (( $# > 0 )); then
echo "$*" | prefix "$p" | log
else
tee | prefix "$p" | log
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment