Skip to content

Instantly share code, notes, and snippets.

@manuq
Created July 28, 2015 15:35
Show Gist options
  • Save manuq/e7bcc0a5c9dfacb80dcb to your computer and use it in GitHub Desktop.
Save manuq/e7bcc0a5c9dfacb80dcb to your computer and use it in GitHub Desktop.
###############################################################################
#
# Bash helper functions for logging.
#
# Inspired in Python logging. Ready to source it in your bash script.
# It can redirect all the output to a file and also redirect all the
# trace to another file, which is useful for debugging.
#
# Usage: see how to use it in the test at the bottom.
#
###############################################################################
# This is free and unencumbered software released into the public domain.
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
setup_loglevel() {
case $1 in
debug)
export RL_LOG_LEVEL=1 ;;
info)
export RL_LOG_LEVEL=2 ;;
warning)
export RL_LOG_LEVEL=3 ;;
error)
export RL_LOG_LEVEL=4 ;;
esac
}
export RL_LOG_LEVEL=2
setup_logfile() {
exec > >(tee -a $1)
exec 2> >(tee -a $1 >&2)
log_debug "Logging to $1"
}
setup_logtrace() {
exec 199>>$1
export BASH_XTRACEFD=199
set -x
log_debug "Writting bash trace to $1"
}
log_debug() {
if [ "$RL_LOG_LEVEL" -le "1" ]; then
echo -e "`date +%s`\tDEBUG\t$1"
fi
}
log_info() {
if [ "$RL_LOG_LEVEL" -le "2" ]; then
echo -e "`date +%s`\tINFO\t$1"
fi
}
log_warning() {
if [ "$RL_LOG_LEVEL" -le "3" ]; then
echo -e "`date +%s`\tWARNING\t$1"
fi
}
log_error() {
if [ "$RL_LOG_LEVEL" -le "4" ]; then
echo -e "`date +%s`\tERROR\t$1" >&2
fi
}
test() {
setup_loglevel "debug"
setup_logfile "./test.log"
setup_logtrace "./test.trace"
log_info "Hello Ripley"
log_warning "Xenomorph detected in area 32"
log_info "All doors locked"
log_error "Engines malfunctioning"
log_debug "Energy 12%"
log_debug "Energy 4%"
log_info "System shutdown in 3... 2... 1..."
}
# test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment