-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
55 lines (48 loc) · 978 Bytes
/
utils.go
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
package l2gp
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
)
type entry struct {
Message string `json:"message"`
Severity severity `json:"severity,omitempty"`
}
type severity string
const (
severityInfo severity = "INFO"
severityError severity = "ERROR"
)
func outputErrorLog(w http.ResponseWriter, statusCode int, format string, v ...interface{}) {
e := entry{
Severity: severityError,
Message: fmt.Sprintf(format, v...),
}
out, err := json.Marshal(e)
if err != nil {
log.Printf("json.Marshal: %v", err)
}
fmt.Println(string(out))
w.WriteHeader(statusCode)
w.Write(out)
}
func outputLog(format string, v ...interface{}) {
e := entry{
Severity: severityInfo,
Message: fmt.Sprintf(format, v...),
}
out, err := json.Marshal(e)
if err != nil {
log.Printf("json.Marshal: %v", err)
}
fmt.Println(string(out))
}
func mustGetEnv(envName string) string {
env := os.Getenv(envName)
if env == "" {
log.Fatal("")
}
return env
}