-
Notifications
You must be signed in to change notification settings - Fork 0
/
lager
executable file
·245 lines (231 loc) · 8.24 KB
/
lager
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
242
243
244
245
#!/usr/bin/env bash
#!/usr/bin/env bash
Usage() { cat <<'EOL'
_________/\\\_______________/\\\\\\\ _______/\\\\\\\\ ___/\\\\\\\\\\\ __/\\\\\\\\\\\
_________\/\\\ ____________/\\\////\\\ ____/\\\/////\\\ _\/\\\/////// __\/\\\//////\\\
__________\/\\\ __________/\\\ ___\/\\\ ___\/\\\ __\///___\/\\\ _________\/\\\ ___/\\\
___________\/\\\ _________\/\\\\\\\\\\\\\ __\/\\\ _________\/\\\\\\\\\ ___\/\\\\\\\\\/
____________\/\\\ _________\/\\\///////\\\ __\/\\\ _/\\\\\ _\/\\\/////_____\/\\\////\\\\
_____________\/\\\ _________\/\\\ ____\/\\\ __\/\\\ \///\\\ _\/\\\ _________\/\\\ _\///\\\
______________\/\\\ _________\/\\\ ____\/\\\ __\/\\\ __\/\\\ _\/\\\ _________\/\\\ ___\//\\\
_______________\/\\\\\\\\\\\ _\/\\\ ____\/\\\ __\//\\\\\\\\/ __\/\\\\\\\\\\ __\/\\\ ____\/\\\
USAGE __________\///////////___\/// _____\///_____\////////_____\//////////____\///______\///
lager.sh <start|stop> [<option>] [<route ...>]
SYNOPSIS
Activate/deactivate logging for optionally specifiable route(s).
After logging has been activated,
the default behavior is to display a simple log-count monitor.
PARAMETERS
install: install middleware and route loggers.
uninstall: uninstall middleware and route loggers.
start: activate logging for [specified||all] routes.
stop : manually deactivate logging for [specified||all] routes.
`route(s)`: relative path(s) to files in the routes directory.
OPTIONS
-h, --help : reveal this helpful usage information.
-a, --all : activate|deactive logging for all routes/endpoints.
-q, --quiet : do not display monitor after activating logging.
-s, --status : get current log counts and list of actively logged routes.
-m, --monitor: activate monitoring mode.
NOTES
* to [un]install: be in project root directory, call script from relative path (`../../wherever/lager install`)
* logging is automatically deactivated during cleanup of the monitoring process.
* if invoked with the `-q` flag, do not forget to manually deactivate with `stop`.
* the clock displayed during monitoring will be set to UTC (or the server's locale)
this can be adjusted by passing the desired timezone as an environment variable
at runtime. for example: ` TZ='America/New_York' ./lager.sh ... `
` TZ='America/Kentucky/Louisville' ./lager.sh ... `
EOL
exit ${1:-0}
}
[[ "${1,,}" != +(\-)@(h)?(elp) ]] || Usage
_viewer() {
#echo 'USAGE: lager view [<OPTIONS>] <ROUTE> [<ENDPOINT>] *WIP'
#echo ' (dir) (log file)'
viewerUsage() {
echo 'USAGE: lager view <LOGFILE> (.logs/...)'
echo 'OPTIONS:'
echo ' -p : pretty print (default is line-oriented (`jq -c`))'
echo ' -n <NUMBER> : only print the last <N> entries'
echo ''
}
viewReq() {
local -i n=${1}
if [[ "${3:-}" == 'Pretty' ]]; then
if ((n == 0)); then
jq '.' -- "$2"
else
tail -n${n} "$2" | jq '.'
fi
else
if ((n == 0)); then
jq -c '.' -- "$2"
else
tail -n${n} "$2" | jq -c '.'
fi
fi
}
viewRes() {
local -i i=${1}
if ((i == 0)); then
jq '.' -- "$2"
else
tail -n${i} "$2" | jq '.'
fi
}
viewPrettyRes() {
local -i len=$(wc -l < "$2")
((len != 0)) || return 1
local -i n=${1}
if ((n==0)); then
# whole file
((n = 1))
else
# last <n> lines
(( n = len - n ))
(( n = n <= 0 ? 1 : n + 1 ))
fi
for((i=n;i<=len;i++)){
sed -n "${i}p" "$2" | jq '.timestamp, .method, .url'
sed -n "${i}p" "$2" | jq -r '.responseBody' | jq -c
echo ""
}
}
local Log='' Kind=''
local -i N=0
for((x=1;x<=$#;x++)){
[[ "${!x,,}" != *(\-)@(h)?(elp) ]] || { viewerUsage; break; }
case "${!x}" in
'-n') ((++x)); [[ "${!x}" != +([[:digit:]]) ]] || N=${!x} ;;
'-p'|'--pretty') Kind='Pretty' ;;
*) [[ ! -f "${!x}" ]] || Log="${!x}"
esac
}
[[ -f "$Log" ]] || return 1
if [[ "${Log,,}" == *'responses.json' ]]; then
view${Kind}Res $N "$Log"
else
viewReq $N "$Log" ${Kind:-}
fi
}
ALL=false
QUIET=false
declare -ga Routes=()
unset Action 2>/dev/null
mkdir -p .logs
## activate|deactivate logging for a given route/endpoint
enablager() { sed -i '/Lager/s/^\/\///' "$1"; }
disablager() { sed -i '/Lager/s/^/\/\//' "$1"; }
_start() { grep -sIqE "^[^\/\/].*(Request|Response)Lager" "$1" || enablager "$1"; }
_stop() { grep -qvIsE "^[^\/\/].*(Request|Response)Lager" "$1" || disablager "$1"; }
## returns list of logfiles
countLogs() { find $1 -type f -name "*.log" -exec wc -l '{}' \; | awk '!/^0/ && !/lager/ { print $0 }' | sort -k 2 | column -t; }
## updates .logs/lager.log with the current log counts (same info displayed during monitoring)
updateCount() { { date ; countLogs '.logs/' ; echo "" ; } | tee .logs/lager.log ; }
## deactivate logging on exit (unless ran with `-q`)
onExit() {
tput reset
for((r=0;r<${#Routes[@]};r++)){ _stop "${Routes[r]}"; }
updateCount >/dev/null
exit 0
}
trap onExit SIGINT
## generates log counts
showLogs() {
local -i i=0 x=$(shize x)
while read line; do
tput cup $((++i)) 0; printf "${line}%$((x-${#line}-1))s\n"
done < <(if ((${#Routes[@]} != 0)); then
for((r=0;r<${#Routes[@]};r++)){
routeName="$(basename -s '.js' ${Routes[r]})";
mkdir -p .logs/${routeName}
countLogs ".logs/${routeName}/"
}
else countLogs '.logs/'; fi
)
}
## reports which if any routes are being monitored
chkStatus() {
updateCount
local -i i=0
local -a Active=()
for r in routes/*.js ; do ((++i))
grep -qvIsE "^[^\/].*(Request|Response)Lager" $r >/dev/null || Active[${#Active[@]}]="$(basename -s '.js' $r)"
done
case ${#Active[@]} in
0) printf '\nNo routes are being logged.\n' ;;
$i) printf '\nActively logging all routes.\n' ;;
*)
printf 'Actively logging:\n'
for((r=0;r<${#Active[@]};r++)){ echo "${Active[r]}"; }
esac
exit 0
}
## display the real-time log monitor
Monitor() {
tput clear civis
while :; do
tput setaf 3 cup 0 0; date; tput sgr0
showLogs; tput sgr0
sleep 3
done
}
iRoutes() {
local GREP_OPTIONS='-m1 -sHIniE' GREP_TARGET="${1:-routes}"
[[ -d "$1" || $# == 0 && -d routes ]] && GREP_OPTIONS+='R'
[[ ! -e $GREP_TARGET ]] || { grep $GREP_OPTIONS '\s*const\s*router\s*\=\s*express\.' "$GREP_TARGET" | awk -F\: '!a[$1]++ {print $2,$1}'; }
}
uRoutes() { grep -m1 -slIR "Lager" routes ; }
insertLager() { local -ai x=( $1 $1 ); local -a Insertions;
Insertions[0]="import { RequestLager, ResponseLager } from '../middlewares/Lager.js';"
Insertions[1]="router.use(RequestLager('${2}'));"
Insertions[2]="router.use(ResponseLager('${2}'));"
! { sed -n "$((++x[1]))p" "$3" | grep -Esq "(r|R)outer\.use\(cors"; } || ((++x[1]))
sed -i.bak "$((++x[1]))i${Insertions[1]}\n${Insertions[2]}" "$3"
sed -i "$((x>1?x-1:1))i${Insertions[0]}" "$3"
}
installagers() {
local lagerSource="$(realpath ${BASH_SOURCE[0]})"
cp -vf ${lagerSource%\/*}/middleware/Lager.js middlewares/Lager.js || exit 2
while read n f ; do
cp -vf "$f" "${f%\/*}/.${f##*\/}"
insertLager $n "$(basename -s '.js' "$f")" "$f"
done < <(iRoutes ${1:-routes} )
echo $'\n'{"middlewares/Lager.js",".logs","routes/*.bak",} >> .gitignore
[[ -x $(which lager) ]] 2>/dev/null || { cp -fv "${lagerSource}" ./ ; chmod +x lager ; }
}
uninstallagers() {
for r in $(grep -m1 -lIRs "Lager" routes); do sed -i '/Lager/d' "$r"; done
rm -f middlewares/Lager.js
[[ ! -f lager ]] || rm -f lager
}
## parse commandline options
for((a=1;a<=$#;a++)){
case "${!a,,}" in
'-v'|*'view') Action='viewer' && break ;;
'stop'|'--stop') Action='stop' ;;
'start'|'--start') Action='start' ;;
'-i'|'--install'|'install') Action='installagers' ;;
\-[ru]|*'remove'|*'uninstall') Action='uninstallagers' ;;
'-a'|*'all') Routes=(routes/*.js) ;;
'-q'|*'quiet') QUIET=true ;;
'-s'|*'status') Action='chkStatus' ;;
'-m'|*'monitor') Action='Monitor' ;;
*)
[[ -f "${!a}" ]] && Routes[${#Routes[@]}]="${!a}" || {
[[ -f "routes/${!a}" ]] && Routes[${#Routes[@]}]="routes/${!a}" || {
[[ ! -f "routes/${!a}.js" ]] || Routes[${#Routes[@]}]="routes/${!a}.js"
}
}
esac
}
## perform action(s)
[[ -n ${Action} ]] || Usage 1
case "${Action}" in
'viewer') _viewer ${@:a+1} ;;
'chkStatus'|'Monitor'|'installagers'|'uninstallagers') $Action ;;
*)
((${#Routes[@]} != 0)) || Usage 1
for((r=0;r<${#Routes[@]};r++)){ _${Action} "${Routes[r]}"; }
${QUIET} || { [[ "$Action" == 'stop' ]] || Monitor ; }
esac