├── LICENSE ├── README.md ├── log4bash.sh └── tests └── bashful.sh /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009-2011, Fred Palmer and contributors. 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | 13 | Neither the name of Fred Palmer nor the names of its contributors may be used 14 | to endorse or promote products derived from this software without specific 15 | prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 19 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 21 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # log4bash 2 | 3 | Let's face it - plain old **echo** just doesn't cut it. **log4bash** is an attempt to have better logging for Bash scripts (i.e. make logging in Bash suck less). 4 | 5 | ## Contributors 6 | 7 | Fred Palmer 8 | 9 | ## Using log4bash 10 | 11 | **source** the *log4bash.sh* script at the beginning of any Bash program. 12 | 13 | ``` bash 14 | 15 | #!/usr/bin/env bash 16 | source log4bash.sh 17 | 18 | log "This is regular log message... log and log_info do the same thing"; 19 | 20 | log_warning "Luke ... you turned off your targeting computer"; 21 | log_info "I have you now!"; 22 | log_success "You're all clear kid, now let's blow this thing and go home."; 23 | log_error "One thing's for sure, we're all gonna be a lot thinner."; 24 | 25 | # If you have figlet installed -- you'll see some big letters on the screen! 26 | log_captains "What was in the captain's toilet?"; 27 | 28 | # If you have the "say" command (e.g. on a Mac) 29 | log_speak "Resistance is futile"; 30 | 31 | ``` 32 | 33 | ## An Overview of log4bash 34 | 35 | 36 | ### Colorized Output 37 | 38 | [![](https://img.skitch.com/20110526-46e6ng8hj11pshw2s5my7e841.jpg)](https://img.skitch.com/20110526-46e6ng8hj11pshw2s5my7e841.jpg) 39 | 40 | ### Logging Levels 41 | 42 | * **log_info** 43 | 44 | Prints an "INFO" level message to stdout with the timestamp of the occurrence. 45 | 46 | * **log_warning** 47 | 48 | Prints a "WARNING" level message to stdout with the timestamp of the occurrence. 49 | 50 | * **log_success** 51 | 52 | Prints a "SUCCESS" level message to stdout with the timestamp of the occurrence. 53 | 54 | * **log_error** 55 | 56 | Prints an "ERROR" level message to stdout with the timestamp of the occurrence. 57 | 58 | ### Special Logging Abilities 59 | 60 | * **log_speak** 61 | 62 | On the Mac platform this will use the say command to echo the command to the current audio output device. 63 | 64 | * **log_captains** 65 | 66 | If the *figlet* program is installed this will print out an ascii-art depiction of the phrase passed to the function. 67 | 68 | * **log_campfire** 69 | 70 | Posts a message to your campfire configured by setting the variables for **CAMPFIRE_API_AUTH_TOKEN** and **CAMPFIRE_NOTIFICATION_URL**. 71 | 72 | ### Other Useful Tidbits 73 | 74 | * **SCRIPT_ARGS** 75 | 76 | A global array of all the arguments used to create run your script 77 | 78 | * **SCRIPT_NAME** 79 | 80 | The script name (sometimes tricky to get right depending on how one invokes a script). 81 | 82 | * **SCRIPT_BASE_DIR** 83 | 84 | The script's base directory which is not always the current working directory. 85 | 86 | ### More to Come 87 | 88 | Please add any feature requests to a ticket for this project. 89 | 90 | -------------------------------------------------------------------------------- /log4bash.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | #-------------------------------------------------------------------------------------------------- 3 | # log4bash - Makes logging in Bash scripting suck less 4 | # Copyright (c) Fred Palmer 5 | # Licensed under the MIT license 6 | # http://github.com/fredpalmer/log4bash 7 | #-------------------------------------------------------------------------------------------------- 8 | set -e # Fail on first error 9 | 10 | # Useful global variables that users may wish to reference 11 | SCRIPT_ARGS="$@" 12 | SCRIPT_NAME="$0" 13 | SCRIPT_NAME="${SCRIPT_NAME#\./}" 14 | SCRIPT_NAME="${SCRIPT_NAME##/*/}" 15 | SCRIPT_BASE_DIR="$(cd "$( dirname "$0")" && pwd )" 16 | 17 | # This should probably be the right way - didn't have time to experiment though 18 | # declare -r INTERACTIVE_MODE="$([ tty --silent ] && echo on || echo off)" 19 | declare -r INTERACTIVE_MODE=$([ "$(uname)" == "Darwin" ] && echo "on" || echo "off") 20 | 21 | #-------------------------------------------------------------------------------------------------- 22 | # Begin Help Section 23 | 24 | HELP_TEXT="" 25 | 26 | # This function is called in the event of an error. 27 | # Scripts which source this script may override by defining their own "usage" function 28 | usage() { 29 | echo -e "${HELP_TEXT}"; 30 | exit 1; 31 | } 32 | 33 | # End Help Section 34 | #-------------------------------------------------------------------------------------------------- 35 | 36 | #-------------------------------------------------------------------------------------------------- 37 | # Begin Logging Section 38 | if [[ "${INTERACTIVE_MODE}" == "off" ]] 39 | then 40 | # Then we don't care about log colors 41 | declare -r LOG_DEFAULT_COLOR="" 42 | declare -r LOG_ERROR_COLOR="" 43 | declare -r LOG_INFO_COLOR="" 44 | declare -r LOG_SUCCESS_COLOR="" 45 | declare -r LOG_WARN_COLOR="" 46 | declare -r LOG_DEBUG_COLOR="" 47 | else 48 | declare -r LOG_DEFAULT_COLOR="\033[0m" 49 | declare -r LOG_ERROR_COLOR="\033[1;31m" 50 | declare -r LOG_INFO_COLOR="\033[1m" 51 | declare -r LOG_SUCCESS_COLOR="\033[1;32m" 52 | declare -r LOG_WARN_COLOR="\033[1;33m" 53 | declare -r LOG_DEBUG_COLOR="\033[1;34m" 54 | fi 55 | 56 | # This function scrubs the output of any control characters used in colorized output 57 | # It's designed to be piped through with text that needs scrubbing. The scrubbed 58 | # text will come out the other side! 59 | prepare_log_for_nonterminal() { 60 | # Essentially this strips all the control characters for log colors 61 | sed "s/[[:cntrl:]]\[[0-9;]*m//g" 62 | } 63 | 64 | log() { 65 | local log_text="$1" 66 | local log_level="$2" 67 | local log_color="$3" 68 | 69 | # Default level to "info" 70 | [[ -z ${log_level} ]] && log_level="INFO"; 71 | [[ -z ${log_color} ]] && log_color="${LOG_INFO_COLOR}"; 72 | 73 | echo -e "${log_color}[$(date +"%Y-%m-%d %H:%M:%S %Z")] [${log_level}] ${log_text} ${LOG_DEFAULT_COLOR}"; 74 | return 0; 75 | } 76 | 77 | log_info() { log "$@"; } 78 | 79 | log_speak() { 80 | if type -P say >/dev/null 81 | then 82 | local easier_to_say="$1"; 83 | case "${easier_to_say}" in 84 | studionowdev*) 85 | easier_to_say="studio now dev ${easier_to_say#studionowdev}"; 86 | ;; 87 | studionow*) 88 | easier_to_say="studio now ${easier_to_say#studionow}"; 89 | ;; 90 | esac 91 | say "${easier_to_say}"; 92 | fi 93 | return 0; 94 | } 95 | 96 | log_success() { log "$1" "SUCCESS" "${LOG_SUCCESS_COLOR}"; } 97 | log_error() { log "$1" "ERROR" "${LOG_ERROR_COLOR}"; log_speak "$1"; } 98 | log_warning() { log "$1" "WARNING" "${LOG_WARN_COLOR}"; } 99 | log_debug() { log "$1" "DEBUG" "${LOG_DEBUG_COLOR}"; } 100 | log_captains() { 101 | if type -P figlet >/dev/null; 102 | then 103 | figlet -f computer -w 120 "$1"; 104 | else 105 | log "$1"; 106 | fi 107 | 108 | log_speak "$1"; 109 | 110 | return 0; 111 | } 112 | 113 | log_campfire() { 114 | # This function performs a campfire notification with the arguments passed to it 115 | if [[ -z ${CAMPFIRE_API_AUTH_TOKEN} || -z ${CAMPFIRE_NOTIFICATION_URL} ]] 116 | then 117 | log_warning "CAMPFIRE_API_AUTH_TOKEN and CAMPFIRE_NOTIFICATION_URL must be set in order log to campfire." 118 | return 1; 119 | fi 120 | 121 | local campfire_message=" 122 | { 123 | \"message\": { 124 | \"type\":\"TextMessage\", 125 | \"body\":\"$@\" 126 | } 127 | }" 128 | 129 | curl \ 130 | --write-out "\r\n" \ 131 | --user ${CAMPFIRE_API_AUTH_TOKEN}:X \ 132 | --header 'Content-Type: application/json' \ 133 | --data "${campfire_message}" \ 134 | ${CAMPFIRE_NOTIFICATION_URL} 135 | 136 | return $?; 137 | } 138 | 139 | # End Logging Section 140 | #-------------------------------------------------------------------------------------------------- 141 | 142 | -------------------------------------------------------------------------------- /tests/bashful.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | #-------------------------------------------------------------------------------------------------- 3 | # log4bash - Makes logging in Bash scripting suck less 4 | # Copyright (c) Fred Palmer 5 | # Licensed under the MIT license 6 | # http://github.com/fredpalmer/log4bash 7 | #-------------------------------------------------------------------------------------------------- 8 | source ../log4bash.sh 9 | 10 | log "This is regular log message... "; 11 | 12 | log_info "So is this..."; 13 | 14 | log_success "Yeah!! Awesome Possum."; 15 | 16 | log_warning "Luke ... you turned off your targeting computer"; 17 | 18 | log_error "Whoops! I made a booboo"; 19 | 20 | # If you have figlet installed -- you'll see some big letters on the screen! 21 | log_captains "What was in the captain's toilet?"; 22 | 23 | # If you have the "say" command (e.g. on a Mac) 24 | log_speak "Resistance is futile"; 25 | --------------------------------------------------------------------------------