-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.sh
241 lines (211 loc) · 5.77 KB
/
logging.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env bash
declare -A LOGGING_LEVELS=([DEBUG]=0 [debug]=0 [INFO]=1 [info]=1 [WARN]=2 [warn]=2 [WARNING]=2 [warning]=2 [ERR]=3 [err]=3 [ERROR]=3 [error]=3)
_stats_base() {
echo "time=\"$datetime\" level=\"$level\" app=\"$app\""
}
_stats_info() {
echo $(_stats_base)
# echo "msg:"
}
_stats_err() {
echo $(_stats_base)
echo "file=\"${BASH_SOURCE[$caller_index]}\" line=\"${BASH_LINENO[$caller_index - 1]}\""
}
_stats_debug() {
echo $(_stats_base)
echo "file=\"${BASH_SOURCE[$caller_index]}\" line=\"${BASH_LINENO[$caller_index - 1]}\" function=\"${FUNCNAME[$caller_index]}\""
}
get_stats() {
local datetime="$(date '+%Y-%m-%d %H:%M:%S.%4N')" # on alpine install coreutils package to get nanoseconds %N
local app="${0##*/}"
local level="${1:-user.info}"
# take care to adjust the index appropriate to caller stack depth
local caller_index=4
# https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html
# line numbers are stored in the array BASH_LINENO -> "${BASH_LINENO[*]}"
# called function in calling order: FUNCNAME -> "${FUNCNAME[*]}"
# echo "function=\"${FUNCNAME[$caller_index]}\""
# echo "functions=\"${FUNCNAME[*]}\""
# echo "lines=\"${BASH_LINENO[*]}\""
case "$level" in
user.info)
echo $(_stats_info)
;;
user.debug)
echo $(_stats_debug)
;;
user.err)
echo $(_stats_err)
;;
user.warn)
echo $(_stats_info)
;;
user.notice)
echo "time=\"$datetime\""
;;
*)
echo $(_stats_info)
;;
esac
}
count_params() {
local optarg_value="$1"
# in case of paramless value, OPTARG will be the next parameter with leading '-' char
if [ "${optarg_value:0:1}" = "-" ]; then
echo 1
else
echo 2
fi
}
_log_usage() {
local function_name="${FUNCNAME[-1]:-log}"
cat <<EOF
Usage:
${function_name:-log} [OPTIONS] [message(s)]
Enhanced 'echo' to log messages with extra infos
Options
-h, --help show's usage information
-M, --mode the programm to use: 'echo' or 'logger'
-X, --no-stats excludes statistics, e.g. time, line, function, etc.
-p, --priority logger priority
-L, --level level in the LOGGING_LEVELS array
Setting the global variable LOGGING_LEVEL, you can specify if a message should be displayed.
e.g. LOGGING_LEVEL=err -> only the highest logging function will display anything
Levels:
* level 0: DEBUG, debug
* level 1: INFO, info
* level 2: WARN, WARNING, warn, warning
* level 3: ERR, ERROR, err, error
EOF
}
log() {
# translate long options to short
args=
for arg; do
delim=""
case "$arg" in
--help) args="${args}-h " ;;
--mode) args="${args}-M " ;;
--priority) args="${args}-p " ;;
--level) args="${args}-L " ;;
--no-stats) args="${args}-X " ;;
# pass through anything else
*)
[[ "${arg:0:1}" == "-" ]] || delim="\""
args="${args}${delim}${arg}${delim} "
;;
esac
done
# reset the translated args
eval set -- $args
# https://eklitzke.org/using-local-optind-with-bash-getopts
local OPTIND ignore
local invalid_opts=0
local valid_opts=0
local local_logging_level=0
local global_logging_level="${LOGGING_LEVEL:-DEBUG}"
local mode=$(_validate_logging_mode "${LOGGING_MODE:-LOGGER}")
local show_stats=$(_validate_logging_stats "${LOGGING_STATS:-1}")
while getopts ":hM:Xp:L:" opt; do
case $opt in
L)
params=$(count_params "${OPTARG}")
valid_opts=$((valid_opts + params))
local_logging_level="${OPTARG}"
;;
M)
params=$(count_params "${OPTARG}")
valid_opts=$((valid_opts + params))
mode=$(_validate_logging_mode "${OPTARG}")
;;
p)
params=$(count_params "${OPTARG}")
valid_opts=$((valid_opts + params))
# needs to be logger priority
priority="${OPTARG:-user.info}"
;;
X)
show_stats=0
valid_opts=$((valid_opts + 1))
;;
h)
_log_usage
return
;;
*)
# echo "invalid option * $OPTARG"
# count invalid options, e.g. log_info "-d test test"
# use them as message
invalid_opts=$((invalid_opts + 1))
;;
esac
done
# shift "$((OPTIND - 1))"
shift "$valid_opts"
local stats="$(get_stats $priority)" # name of programm
local msg="$@" # use all the rest as message
# check, if we should print messages, depending on global logging level order
if ((${LOGGING_LEVELS[$global_logging_level]:-0} > local_logging_level)); then
return
fi
case $mode in
logger)
if [ "${show_stats}" -eq 1 ]; then
logger -s -p $priority "$stats msg=\"$msg\""
else
logger -s -p $priority "msg=\"$msg\""
fi
;;
echo)
if [ "${show_stats}" -eq 1 ]; then
echo >&2 "priority=\"$priority\" $stats msg=\"$msg\""
else
echo >&2 "$msg"
fi
;;
*)
logger -s -p $priority "$stats msg=\"$msg\""
;;
esac
}
_validate_logging_mode() {
local mode="${1:-logger}"
if [ $mode = "logger" ] || [ $mode = "echo" ]; then
echo $mode
else
echo "logger"
fi
return
}
_validate_logging_stats() {
local show_stats="${1:-1}"
if [ "${show_stats}" -eq 1 ]; then
echo 1
else
echo 0
fi
}
# level 0
log_debug() {
log --level 0 --priority user.debug "$@"
}
# level 1
log_info() {
log --level 1 --priority user.info "$@"
}
log_warn() {
log --level 2 --priority user.warn "$@"
}
log_warning() {
log --level 2 --priority user.warn "$@"
}
log_err() {
log --level 3 --priority user.err "$@"
}
log_error() {
log --level 3 --priority user.err "$@"
}
# show always
log_notice() {
log --level 4 --priority user.notice "$@"
}