-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliberlogger.go
86 lines (74 loc) · 1.5 KB
/
liberlogger.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
package liberlogger
import (
"fmt"
"net/http"
"strings"
"github.com/rs/zerolog"
)
const (
REDACTED = "REDACTED"
)
var DefaultKeys = []string{
"access_token",
"client_id",
"client_secret",
"Authorization",
"password",
}
var DefaultKeysToMask = []string{
"document",
"cpf",
"cnpj",
}
func extraLogs(request interface{}, err error) *zerolog.Event {
log := zerolog.Dict()
if err != nil {
log.Interface("error", err.Error())
}
switch request := request.(type) {
case *http.Request:
log.
Interface("url", request.URL.String()).
Interface("method", request.Method)
case *http.Response:
log.
Interface("request", map[string]interface{}{
"url": request.Request.URL.String(),
"method": request.Request.Method,
})
}
return log
}
func formatFinalMsg(request interface{}, msg string) (finalMsg string) {
switch request := request.(type) {
case *http.Request:
finalMsg = fmt.Sprintf("%s %s %s",
msg,
request.Method,
request.URL.String(),
)
case *http.Response:
finalMsg = fmt.Sprintf("%s %s %d %s",
msg,
request.Request.Method,
request.StatusCode,
request.Request.URL.String(),
)
case *LogResponseWriter:
finalMsg = fmt.Sprintf("%s %s %d %s",
msg,
request.Request.Method,
request.StatusCode,
request.Request.URL.String(),
)
}
return
}
func ignoreRoute(ignoreList []string, request *http.Request) bool {
for _, ignore := range ignoreList {
if strings.Compare(ignore, request.RequestURI) == 0 {
return true
}
}
return false
}