/
http.go
65 lines (55 loc) · 1.42 KB
/
http.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
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"net/http"
"os"
"strings"
"sync"
"time"
)
func newLogger(t http.RoundTripper) http.RoundTripper {
return &loggingTransport{transport: t}
}
type loggingTransport struct {
transport http.RoundTripper
mu sync.Mutex
active []byte
}
func (t *loggingTransport) RoundTrip(r *http.Request) (*http.Response, error) {
t.mu.Lock()
index := len(t.active)
start := time.Now()
fmt.Fprintf(os.Stderr, "HTTP: %s %s+ %s\n", timeFormat1(start), t.active, r.URL)
t.active = append(t.active, '|')
t.mu.Unlock()
resp, err := t.transport.RoundTrip(r)
last := r.URL.Path
if i := strings.LastIndex(last, "/"); i >= 0 {
last = last[i:]
}
display := last
if resp != nil {
display += " " + resp.Status
}
if err != nil {
display += " error: " + err.Error()
}
now := time.Now()
t.mu.Lock()
t.active[index] = '-'
fmt.Fprintf(os.Stderr, "HTTP: %s %s %s (%.3fs)\n", timeFormat1(now), t.active, display, now.Sub(start).Seconds())
t.active[index] = ' '
n := len(t.active)
for n%4 == 0 && n >= 4 && t.active[n-1] == ' ' && t.active[n-2] == ' ' && t.active[n-3] == ' ' && t.active[n-4] == ' ' {
t.active = t.active[:n-4]
n -= 4
}
t.mu.Unlock()
return resp, err
}
func timeFormat1(t time.Time) string {
return t.Format("15:04:05.000")
}