-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger.go
103 lines (89 loc) · 2.19 KB
/
logger.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package roundtripware
import (
"net/http"
"go.uber.org/zap"
"github.com/foomo/keel/log"
keeltime "github.com/foomo/keel/time"
)
type (
LoggerOptions struct {
Message string
ErrorMessage string
MinWarnCode int
MinErrorCode int
}
LoggerOption func(*LoggerOptions)
)
// GetDefaultLoggerOptions returns the default options
func GetDefaultLoggerOptions() LoggerOptions {
return LoggerOptions{
Message: "sent request",
ErrorMessage: "failed to sent request",
MinWarnCode: 400,
MinErrorCode: 500,
}
}
// LoggerWithMessage middleware option
func LoggerWithMessage(v string) LoggerOption {
return func(o *LoggerOptions) {
o.Message = v
}
}
// LoggerWithErrorMessage middleware option
func LoggerWithErrorMessage(v string) LoggerOption {
return func(o *LoggerOptions) {
o.ErrorMessage = v
}
}
// LoggerWithMinWarnCode middleware option
func LoggerWithMinWarnCode(v int) LoggerOption {
return func(o *LoggerOptions) {
o.MinWarnCode = v
}
}
// LoggerWithMinErrorCode middleware option
func LoggerWithMinErrorCode(v int) LoggerOption {
return func(o *LoggerOptions) {
o.MinErrorCode = v
}
}
// Logger returns a RoundTripware which logs all requests
func Logger(opts ...LoggerOption) RoundTripware {
o := GetDefaultLoggerOptions()
for _, opt := range opts {
if opt != nil {
opt(&o)
}
}
return func(l *zap.Logger, next Handler) Handler {
return func(req *http.Request) (*http.Response, error) {
start := keeltime.Now()
statusCode := http.StatusTeapot
// extend logger using local instance
l := log.WithHTTPRequestOut(l, req)
// execute next handler
resp, err := next(req)
if err != nil {
l = log.WithError(l, err)
} else if resp != nil {
l = log.With(l,
log.FHTTPStatusCode(resp.StatusCode),
log.FHTTPRequestContentLength(resp.ContentLength),
)
statusCode = resp.StatusCode
}
l = l.With(log.FDuration(keeltime.Now().Sub(start)))
switch {
case err != nil:
l.Error(o.ErrorMessage)
case o.MinErrorCode > 0 && statusCode >= o.MinErrorCode:
l.Error(o.Message)
case o.MinWarnCode > 0 && statusCode >= o.MinWarnCode:
l.Warn(o.Message)
default:
l.Info(o.Message)
}
return resp, err
}
}
}