-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtail-function-logs.sh
More file actions
executable file
·46 lines (40 loc) · 1.69 KB
/
tail-function-logs.sh
File metadata and controls
executable file
·46 lines (40 loc) · 1.69 KB
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
#!/bin/bash
set -e # Fail on any error
set -o pipefail # Ensure piped commands propagate exit codes properly
set -u # Treat unset variables as an error when substituting
# Tails the logs for the Cloud Function in real-time.
# Usage: tail-function-logs.sh [rate_feed]
# Example with rate feed filter: tail-function-logs.sh CELO/USD
tail_function_logs() {
# Load the current project variables
script_dir=$(dirname "$0")
source "${script_dir}/get-project-vars.sh"
# Optional rate feed filter (first argument)
rate_feed="${1-}"
# Base log query
query="(resource.labels.service_name=\"${function_name}\")"
# Add rate feed filter if provided
if [[ -n ${rate_feed} ]]; then
query="${query} AND labels.rateFeed=\"${rate_feed}\""
printf "\nTailing logs for rate feed: \033[1m%s\033[0m\n\n" "${rate_feed}"
else
printf "\nTailing all logs for \033[1m%s\033[0m\n\n" "${function_name}"
fi
# Tail logs in real-time with formatted output
# Parse YAML with compact awk script
gcloud beta logging tail "${query}" \
--project "${project_id}" \
--format "default" 2>&1 |
grep --line-buffered -v -E "(UserWarning|pkg_resources|Initializing tail session)" |
awk -v y="\033[33m" -v r="\033[31m" -v x="\033[0m" '
/^timestamp:/ { t=$2; gsub(/['\''T]/, " ", t); gsub(/\.[0-9]+Z/, "", t) }
/^severity:/ { s=($2=="500"||$2=="ERROR")?"ERROR":($2=="400"||$2=="WARNING")?"WARNING":"INFO" }
/^ rateFeed:/ { f=$2 }
/^ message:/ { sub(/^ message: '\''?/, ""); sub(/'\''$/, ""); m=$0 }
/^text_payload:/ { sub(/^text_payload: '\''?/, ""); sub(/'\''$/, ""); m=$0 }
/^---$/ && t && m {
printf "%s%s%s [%s] [%s]: %s\n", y, t, x, s, (f?f:"N/A"), m; fflush()
t=s=f=m=""
}'
}
tail_function_logs "$@"