forked from ernesto-jimenez/httplogger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
47 lines (39 loc) · 815 Bytes
/
example.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
package main
import (
"log"
"net/http"
"os"
"time"
"github.com/ernesto-jimenez/httplogger"
)
func main() {
client := http.Client{
Transport: httplogger.NewLoggedTransport(http.DefaultTransport, newLogger()),
}
client.Get("http://google.com")
}
type httpLogger struct {
log *log.Logger
}
func newLogger() *httpLogger {
return &httpLogger{
log: log.New(os.Stderr, "log - ", log.LstdFlags),
}
}
func (l *httpLogger) LogRequest(req *http.Request) {
l.log.Printf(
"Request %s %s",
req.Method,
req.URL.String(),
)
}
func (l *httpLogger) LogResponse(req *http.Request, res *http.Response, err error, duration time.Duration) {
duration /= time.Millisecond
l.log.Printf(
"Response method=%s status=%d durationMs=%d %s",
req.Method,
res.StatusCode,
duration,
req.URL.String(),
)
}