-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget-function-logs.sh
More file actions
executable file
·72 lines (60 loc) · 2.1 KB
/
get-function-logs.sh
File metadata and controls
executable file
·72 lines (60 loc) · 2.1 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
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
#!/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
# Fetches the latest logs for the Cloud Function and displays them in the terminal.
# Usage: get-function-logs.sh [rate_feed]
# Example with rate feed filter: get-function-logs.sh CELO/USD
get_function_logs() {
# Load the current project variables and spinner utility
script_dir=$(dirname "$0")
source "${script_dir}/get-project-vars.sh"
source "${script_dir}/spinner.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}\""
fi
# Fetch raw logs with spinner
printf "\n"
if [[ -n ${rate_feed} ]]; then
title=$(printf "Fetching logs for rate feed: \033[1m%s\033[0m\n" "${rate_feed}")
else
title="Fetching logs for all rate feeds"
fi
start_spinner "${title}"
# Run the gcloud command
raw_logs=$(gcloud logging read "${query}" \
--project "${project_id}" \
--format json \
--limit 50)
# Stop spinner
cleanup_spinner
if [[ -n ${rate_feed} ]]; then
printf "Logs filtered by rate feed: \033[1m%s\033[0m\n" "${rate_feed}"
fi
printf "\n"
# Handle both jsonPayload.message and textPayload formats
# Extract message handling any format (string or object)
echo "${raw_logs}" | jq -r 'reverse | .[] |
# Determine the message content
(if .jsonPayload.message then
.jsonPayload.message
elif .textPayload then
.textPayload
elif .jsonPayload then
(.jsonPayload | tostring)
else
""
end) as $message |
# Format the output
if .severity == "ERROR" then
"\u001b[31m[\(.severity)]\u001b[0m \u001b[33m\(.timestamp | sub("T"; " ") | sub("\\..*"; ""))\u001b[0m [\(.labels.rateFeed // "N/A")]: \($message)"
else
"[\(.severity)] \u001b[33m\(.timestamp | sub("T"; " ") | sub("\\..*"; ""))\u001b[0m [\(.labels.rateFeed // "N/A")]: \($message)"
end'
}
get_function_logs "$@"