-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogged.go
53 lines (47 loc) · 1.28 KB
/
logged.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
package cmhttp
import (
"net/http"
"time"
)
// Logged is used to execute a log function after the request has been made.
// Neither the request body nor the response body will be logged.
// If the client returned an error it will be logged as well.
func Logged(logf func(string, ...interface{}), trigger func() bool) Decorator {
if trigger == nil {
trigger = func() bool { return true }
}
return func(c Client) Client {
return ClientFunc(func(r *http.Request) (res *http.Response, err error) {
if !trigger() {
return c.Do(r)
}
defer func(begin time.Time) {
took := time.Since(begin)
if err != nil {
logf(
"cmhttp client error",
"method", r.Method,
"url", r.URL,
"proto", r.Proto,
"request_content_length", r.Header.Get("Content-Length"),
"took_ms", int64(took/time.Millisecond),
"error", err.Error(),
)
} else {
logf(
"cmhttp client response",
"method", r.Method,
"url", r.URL,
"proto", r.Proto,
"request_content_length", r.Header.Get("Content-Length"),
"response_content_length", res.Header.Get("Content-Length"),
"response_status", res.Status,
"took_ms", int64(took/time.Millisecond),
)
}
}(time.Now())
res, err = c.Do(r)
return res, err
})
}
}