Skip to content

Instantly share code, notes, and snippets.

@papaben
Last active April 7, 2016 23:33
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 papaben/2c8d2428444e447657eabd6e4ed6f5a4 to your computer and use it in GitHub Desktop.
Save papaben/2c8d2428444e447657eabd6e4ed6f5a4 to your computer and use it in GitHub Desktop.
Small library exposing logging methods to bash scripts. Output is only written based on log level set by consumer, defaults to all off
##
# Log levels for bash scripts
# vim: set ft=sh :
##
# Date format for logging
declare -r DATE_FORMAT='+%Y-%m-%d:%H:%M:%S:%:z'
##
# These are the "methods" that your script will use to do the logging
# E.g. #1 Write a debug message
# $log_debug "About to remove directory $temp"
# rmdir $temp
# E.g. #2 Write a warning message
# if [[ ! -r $file ]]; then
# $log_warning "Cannot read configuration from $conf"
# fi
##
declare log_debug='_log_silent'
declare log_info='_log_silent'
declare log_warning='_log_silent'
declare log_error='_log_silent'
##
# Boolean for toggling whether to add logging to file
# By default, all logs are only to console
##
declare _log_levels_should_log_to_file=false
declare _log_levels_log_file="/tmp/${0}.log"
##
# Variable to hold additional info for log message
##
declare _log_levels_prefix=''
##
# Use these to set the logging levels as desired
# Typically, this would be done when setting up your script
##
function set_logging_level_debug() {
log_debug='_log_debug'
log_info='_log_info'
log_warning='_log_warning'
log_error='_log_error'
}
function set_logging_level_info() {
log_info='_log_info'
log_warning='_log_warning'
log_error='_log_error'
}
function set_logging_level_warning() {
log_warning='_log_warning'
log_error='_log_error'
}
function set_logging_level_error() {
log_error='_log_error'
}
##
# Enable writing logs to disk
# @param $1 Full path to file where logs are to be stored
##
function set_logging_file_appender() {
if [[ ! -z "${1}" ]]
then
_log_levels_log_file="${1}"
fi
if [[ -w "${_log_levels_log_file}" ]]
then
_log_levels_should_log_to_file=true
else
echo >&2 "Log file ${_log_levels_log_file} not writable"
fi
}
##
# Add additional information to log message
# E.g. set_logging_prefix "${PID} Puppet hooks:"
# @param $1 Full prefix string
##
function set_logging_prefix() {
_log_levels_prefix="${1}"
}
##
# The behind the scenes functions that do the echo'ing
##
function _log_silent() {
:
}
function _log_debug() {
local log_date="$(date "${DATE_FORMAT}")"
local log_message="${log_date} DEBUG ${_log_levels_prefix} $*"
if [[ "${_log_levels_should_log_to_file}" = true ]] ; then
echo "${log_message}" >> "${_log_levels_log_file}"
fi
echo "${log_message}"
}
function _log_info() {
local log_date="$(date "${DATE_FORMAT}")"
local log_message="${log_date} INFO ${_log_levels_prefix} $*"
if [[ "${_log_levels_should_log_to_file}" = true ]] ; then
echo "${log_message}" >> "${_log_levels_log_file}"
fi
echo "${log_message}"
}
function _log_warning() {
local log_date="$(date "${DATE_FORMAT}")"
local log_message="${log_date} WARNING ${_log_levels_prefix} $*"
if [[ "${_log_levels_should_log_to_file}" = true ]] ; then
echo "${log_message}" >> "${_log_levels_log_file}"
fi
echo >&2 "${log_message}"
}
function _log_error() {
local log_date="$(date "${DATE_FORMAT}")"
local log_message="${log_date} ERROR ${_log_levels_prefix} $*"
if [[ "${_log_levels_should_log_to_file}" = true ]] ; then
echo "${log_message}" >> "${_log_levels_log_file}"
fi
echo >&2 "${log_message}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment