-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsf-log
executable file
·149 lines (135 loc) · 6.07 KB
/
sf-log
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
#!/usr/bin/env bash
#
# create or update a debug-log user and print debug-log
#
# USAGE
#
# sf-log # update debug-log for autheticated org user
# sf-log -o ORG # set ORG
# sf-log -u USER # set user [default: the currently autheticated org user]
# sf-log -l DEBUG_LEVEL # set debug-level [default: SFDC_DevConsole]
# sf-log -t MINUTES # set debug-level for MINUTES min [default: 120]
# sf-log --list # list debug-log users
# sf-log --delete # delete debug-logs and debug-log users
#
# EXAMPLES
#
# sf-log
# less debug.log
# tail -f -n +1 debug.log | rg USER_DEBUG
#
# COMMANDS
#
# sf apex list log
# sf apex tail log -c -s | spacer
# script -q /dev/null sf apex tail log -c -s | tee debug.log
yesno() {
echo ""
read -p "$1 [Y/n] " response
[[ $response == "n" || $response == "N" ]] && exit 1
}
show_help() {
awk '/^[^ #]/{c=1}c==0{print $0}' $0 | sed -n '/^#/p' | sed 1d | sed 's/^#/ /g' \
| perl -pe "s/ #(.*)$/$(tput setaf 0)\1$(tput sgr 0)/" \
| perl -pe "s/(USAGE|EXAMPLES|COMMANDS)/$(tput setaf 0)\1$(tput sgr 0)/" \
| perl -pe "s/\`(.+)\`/$(tput sgr 0 1)\1$(tput sgr 0)/"
exit 1
}
setTargetOrg() {
if [[ -z "$org" ]]; then
org=$(cat .sf/config.json 2>/dev/null | jq -r '."target-org" | select( . != null )')
fi
if [[ -z "$org" ]]; then
org=$(cat ~/.sf/config.json 2>/dev/null | jq -r '."target-org" | select( . != null )')
fi
if [[ -z "$org" ]]; then
echo -e >&2 "\n $(tput setaf 1)Missing target org$(tput sgr 0)\n\n Either provide with $(tput setaf 0)-o$(tput sgr 0) option, or set via $(tput setaf 0)sf config set target-org <org>$(tput sgr 0)"
exit 1
fi
}
org=""
user=""
level="SFDC_DevConsole"
delete=false
list=false
time="120"
while [ $# -gt 0 ]; do
case $1 in
-o|--org)
shift
org=$1
;;
-u|--user)
shift
user="$1"
;;
-l|--level)
shift
level="$1"
;;
-t|--time)
shift
time="$1"
;;
--list)
list=true
;;
--delete)
delete=true
;;
-h|--help)
show_help
;;
*)
;;
esac
shift
done
setTargetOrg
if [[ $list == true ]]; then
yesno " List debug-log users on $(tput setaf 1)${org}$(tput sgr0) ?"
echo
while read -a arr; do
from=$(date -v +1H -j -f %Y-%m-%dT%H:%M:%S.000+0000 "${arr[0]}" +%s)
to=$(date -v +1H -j -f %Y-%m-%dT%H:%M:%S.000+0000 "${arr[1]}" +%s)
now=$(date +%s)
[ "$to" -ge "$now" ] && col=2 || col=1
range="$(tput setaf ${col})$(date -j -f %s "$from" "+%d.%m.%Y, %H:%M") → $(date -j -f %s "$to" "+%d.%m.%Y, %H:%M")$(tput sgr0)"
usrname=$(sf data query -q "SELECT Name FROM User WHERE Id='${arr[2]}'" -o ${org} --json | jq -r '.result.records[].Name')
dbglevel=$(sf data query -q "SELECT DeveloperName FROM DebugLevel WHERE Id='${arr[3]}'" -t -o ${org} --json | jq -r '.result.records[].DeveloperName')
echo " $range: $usrname $(tput setaf 0)[${dbglevel}]$(tput sgr0)"
done < <(sf data query -q "SELECT StartDate, ExpirationDate, TracedEntityId, DebugLevelId FROM TraceFlag WHERE LogType='USER_DEBUG'" -t -o ${org} --json | jq -r '.result.records[] | "\(.StartDate) \(.ExpirationDate) \(.TracedEntityId) \(.DebugLevelId)"')
exit 0
fi
if [[ $delete == true ]]; then
yesno " Delete debug-logs and debug-log users on $(tput setaf 1)${org}$(tput sgr0) ?"
echo
list_of_ids=$(mktemp)
echo -e "Id\n$(sf apex list log -o "$org" --json | jq -r '.result[].Id')" > "$list_of_ids"
sf data delete bulk -o "$org" -f "$list_of_ids" -s ApexLog
while read id; do
sf data delete record -o "$org" -t -i "$id" -s TraceFlag > /dev/null
done < <(sf data query -q "SELECT Id FROM TraceFlag WHERE LogType='USER_DEBUG'" -t -o ${org} --json | jq -r '.result.records[].Id')
exit 0
fi
if [[ $user == "" ]]; then
user=$(cat ~/.sfdx/alias.json | jq -r ".orgs.${org}")
fi
traceid=$(sf data query -q "SELECT Id FROM TraceFlag WHERE TracedEntityId IN (SELECT Id FROM User WHERE Username='${user}')" -t -o ${org} --json | jq -r '.result.records[0].Id')
startdate=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
expirationdate=$(date -v "+${time}M" -u +"%Y-%m-%dT%H:%M:%SZ")
if [[ $traceid == "null" ]]; then
yesno " Create new debug log for $(tput setaf 2)${user}$(tput sgr0) with log-level $(tput setaf 2)${level}$(tput sgr0) for $(tput setaf 2)${time} min$(tput sgr0) on $(tput setaf 1)${org}$(tput sgr0) ?"
levelid=$(sf data query -q "SELECT Id FROM DebugLevel WHERE DeveloperName='${level}'" -t -o ${org} --json | jq -r '.result.records[0].Id')
userid=$(sf data query -q "SELECT Id from User WHERE Username='${user}'" -o ${org} --json | jq -r '.result.records[0].Id')
[[ $DEBUG == "1" ]] && echo -e "\n $(tput setaf 0)sf data create record -o ${org} -s TraceFlag -v \"TracedEntityId=${userid} StartDate=${startdate} ExpirationDate=${expirationdate} DebugLevelId=${levelid} LogType=USER_DEBUG\" -t$(tput sgr0)"
sf data create record -o ${org} -s TraceFlag -v "TracedEntityId=${userid} StartDate=${startdate} ExpirationDate=${expirationdate} DebugLevelId=${levelid} LogType=USER_DEBUG" -t --json > /dev/null
else
yesno " Update debug log for $(tput setaf 2)${user}$(tput sgr0) with log-level $(tput setaf 2)${level}$(tput sgr0) for $(tput setaf 2)${time} min$(tput sgr0) on $(tput setaf 1)${org}$(tput sgr0) ?"
levelid=$(sf data query -q "SELECT Id FROM DebugLevel WHERE DeveloperName='${level}'" -t -o ${org} --json | jq -r '.result.records[0].Id')
[[ $DEBUG == "1" ]] && echo -e "\n $(tput setaf 0)sf data update record -o ${org} -s TraceFlag -i ${traceid} -v \"StartDate=${startdate} ExpirationDate=${expirationdate} DebugLevelId=${levelid}\" -t$(tput sgr0)"
sf data update record -o ${org} -s TraceFlag -i ${traceid} -v "StartDate=${startdate} ExpirationDate=${expirationdate} DebugLevelId=${levelid}" -t --json > /dev/null
fi
[[ $DEBUG == "1" ]] && echo -e " $(tput setaf 0)script -q /dev/null sf apex tail log -c -s -o ${org} | tee debug.log$(tput sgr0)\n"
script -q /dev/null sf apex tail log -c -s -o ${org} | tee debug.log
trap "rm debug.log; exit 0;" SIGINT EXIT