forked from nbryant42/rexray
/
client_logging.go
66 lines (51 loc) · 1.11 KB
/
client_logging.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
package client
import (
"bufio"
"bytes"
"fmt"
"net/http"
"net/http/httputil"
log "github.com/sirupsen/logrus"
"github.com/akutz/gotil"
)
func (c *client) logRequest(req *http.Request) {
if !c.logRequests {
return
}
w := log.StandardLogger().Writer()
fmt.Fprintln(w, "")
fmt.Fprint(w, " -------------------------- ")
fmt.Fprint(w, "HTTP REQUEST (CLIENT)")
fmt.Fprintln(w, " -------------------------")
buf, err := httputil.DumpRequest(req, true)
if err != nil {
return
}
gotil.WriteIndented(w, buf)
fmt.Fprintln(w)
}
func (c *client) logResponse(res *http.Response) {
if !c.logResponses {
return
}
w := log.StandardLogger().Writer()
fmt.Fprintln(w)
fmt.Fprint(w, " -------------------------- ")
fmt.Fprint(w, "HTTP RESPONSE (CLIENT)")
fmt.Fprintln(w, " -------------------------")
buf, err := httputil.DumpResponse(
res,
res.Header.Get("Content-Type") != "application/octet-stream")
if err != nil {
return
}
bw := &bytes.Buffer{}
gotil.WriteIndented(bw, buf)
scanner := bufio.NewScanner(bw)
for {
if !scanner.Scan() {
break
}
fmt.Fprintln(w, scanner.Text())
}
}