-
Notifications
You must be signed in to change notification settings - Fork 3
/
logging_log.go
87 lines (68 loc) · 1.56 KB
/
logging_log.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
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
//go:build !go1.21
package fauna
import (
"context"
"fmt"
"log"
"net/http"
"os"
"strconv"
)
type Logger interface {
Debug(msg string)
Info(msg string)
Warn(msg string)
Error(msg string)
LogResponse(ctx context.Context, requestBody []byte, r *http.Response)
}
type ClientLogger struct {
Logger
logger *log.Logger
level int
}
func (d ClientLogger) Debug(msg string) {
if d.logger == nil {
return
}
d.logger.Print("DEBUG: " + msg)
}
func (d ClientLogger) Info(msg string) {
if d.logger == nil {
return
}
d.logger.Print("INFO: " + msg)
}
func (d ClientLogger) Warn(msg string) {
if d.logger == nil {
return
}
d.logger.Print("WARN: " + msg)
}
func (d ClientLogger) Error(msg string) {
if d.logger == nil {
return
}
d.logger.Print("ERROR: " + msg)
}
func (d ClientLogger) LogResponse(ctx context.Context, requestBody []byte, r *http.Response) {
if d.logger == nil {
return
}
headers := r.Request.Header
if _, found := headers["Authorization"]; found {
headers["Authorization"] = []string{"hidden"}
}
d.Debug(fmt.Sprintf("Request Body: %s", string(requestBody)))
d.Info(fmt.Sprintf("HTTP Response - Status: %s, From: %s, Headers: %v", r.Status, r.Request.URL.String(), headers))
}
// DefaultLogger returns the default logger
func DefaultLogger() Logger {
clientLogger := ClientLogger{}
if val, found := os.LookupEnv(EnvFaunaDebug); found {
if level, _ := strconv.Atoi(val); level >= -4 {
clientLogger.level = level
clientLogger.logger = log.New(os.Stdout, "[fauna-go] ", log.LstdFlags|log.Lshortfile)
}
}
return clientLogger
}