-
Notifications
You must be signed in to change notification settings - Fork 48
/
log4bash.sh
executable file
·141 lines (119 loc) · 4.48 KB
/
log4bash.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env bash
#--------------------------------------------------------------------------------------------------
# log4bash - Makes logging in Bash scripting suck less
# Copyright (c) Fred Palmer
# Licensed under the MIT license
# http://github.com/fredpalmer/log4bash
#--------------------------------------------------------------------------------------------------
set -e # Fail on first error
# Useful global variables that users may wish to reference
SCRIPT_ARGS="$@"
SCRIPT_NAME="$0"
SCRIPT_NAME="${SCRIPT_NAME#\./}"
SCRIPT_NAME="${SCRIPT_NAME##/*/}"
SCRIPT_BASE_DIR="$(cd "$( dirname "$0")" && pwd )"
# This should probably be the right way - didn't have time to experiment though
# declare -r INTERACTIVE_MODE="$([ tty --silent ] && echo on || echo off)"
declare -r INTERACTIVE_MODE=$([ "$(uname)" == "Darwin" ] && echo "on" || echo "off")
#--------------------------------------------------------------------------------------------------
# Begin Help Section
HELP_TEXT=""
# This function is called in the event of an error.
# Scripts which source this script may override by defining their own "usage" function
usage() {
echo -e "${HELP_TEXT}";
exit 1;
}
# End Help Section
#--------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------
# Begin Logging Section
if [[ "${INTERACTIVE_MODE}" == "off" ]]
then
# Then we don't care about log colors
declare -r LOG_DEFAULT_COLOR=""
declare -r LOG_ERROR_COLOR=""
declare -r LOG_INFO_COLOR=""
declare -r LOG_SUCCESS_COLOR=""
declare -r LOG_WARN_COLOR=""
declare -r LOG_DEBUG_COLOR=""
else
declare -r LOG_DEFAULT_COLOR="\033[0m"
declare -r LOG_ERROR_COLOR="\033[1;31m"
declare -r LOG_INFO_COLOR="\033[1m"
declare -r LOG_SUCCESS_COLOR="\033[1;32m"
declare -r LOG_WARN_COLOR="\033[1;33m"
declare -r LOG_DEBUG_COLOR="\033[1;34m"
fi
# This function scrubs the output of any control characters used in colorized output
# It's designed to be piped through with text that needs scrubbing. The scrubbed
# text will come out the other side!
prepare_log_for_nonterminal() {
# Essentially this strips all the control characters for log colors
sed "s/[[:cntrl:]]\[[0-9;]*m//g"
}
log() {
local log_text="$1"
local log_level="$2"
local log_color="$3"
# Default level to "info"
[[ -z ${log_level} ]] && log_level="INFO";
[[ -z ${log_color} ]] && log_color="${LOG_INFO_COLOR}";
echo -e "${log_color}[$(date +"%Y-%m-%d %H:%M:%S %Z")] [${log_level}] ${log_text} ${LOG_DEFAULT_COLOR}";
return 0;
}
log_info() { log "$@"; }
log_speak() {
if type -P say >/dev/null
then
local easier_to_say="$1";
case "${easier_to_say}" in
studionowdev*)
easier_to_say="studio now dev ${easier_to_say#studionowdev}";
;;
studionow*)
easier_to_say="studio now ${easier_to_say#studionow}";
;;
esac
say "${easier_to_say}";
fi
return 0;
}
log_success() { log "$1" "SUCCESS" "${LOG_SUCCESS_COLOR}"; }
log_error() { log "$1" "ERROR" "${LOG_ERROR_COLOR}"; log_speak "$1"; }
log_warning() { log "$1" "WARNING" "${LOG_WARN_COLOR}"; }
log_debug() { log "$1" "DEBUG" "${LOG_DEBUG_COLOR}"; }
log_captains() {
if type -P figlet >/dev/null;
then
figlet -f computer -w 120 "$1";
else
log "$1";
fi
log_speak "$1";
return 0;
}
log_campfire() {
# This function performs a campfire notification with the arguments passed to it
if [[ -z ${CAMPFIRE_API_AUTH_TOKEN} || -z ${CAMPFIRE_NOTIFICATION_URL} ]]
then
log_warning "CAMPFIRE_API_AUTH_TOKEN and CAMPFIRE_NOTIFICATION_URL must be set in order log to campfire."
return 1;
fi
local campfire_message="
{
\"message\": {
\"type\":\"TextMessage\",
\"body\":\"$@\"
}
}"
curl \
--write-out "\r\n" \
--user ${CAMPFIRE_API_AUTH_TOKEN}:X \
--header 'Content-Type: application/json' \
--data "${campfire_message}" \
${CAMPFIRE_NOTIFICATION_URL}
return $?;
}
# End Logging Section
#--------------------------------------------------------------------------------------------------