This repository has been archived by the owner on May 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
log.go
131 lines (110 loc) · 2.67 KB
/
log.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package httplog
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"net/http/httputil"
"os"
"strings"
"time"
)
const (
CacheHeader = "X-Cache"
)
type responseWriter struct {
http.ResponseWriter
status int
size int
t time.Time
errorOutput bytes.Buffer
}
func (l *responseWriter) Header() http.Header {
return l.ResponseWriter.Header()
}
func (l *responseWriter) Write(b []byte) (int, error) {
if l.status == 0 {
l.status = http.StatusOK
}
if isError(l.status) {
l.errorOutput.Write(b)
}
size, err := l.ResponseWriter.Write(b)
l.size += size
return size, err
}
func (l *responseWriter) WriteHeader(s int) {
l.ResponseWriter.WriteHeader(s)
l.status = s
}
func (l *responseWriter) Status() int {
return l.status
}
func (l *responseWriter) Size() int {
return l.size
}
func NewResponseLogger(delegate http.Handler) *ResponseLogger {
return &ResponseLogger{Handler: delegate}
}
type ResponseLogger struct {
http.Handler
DumpRequests, DumpErrors, DumpResponses bool
}
func (l *ResponseLogger) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if l.DumpRequests {
b, _ := httputil.DumpRequest(req, false)
writePrefixString(strings.TrimSpace(string(b)), ">> ", os.Stderr)
}
respWr := &responseWriter{ResponseWriter: w, t: time.Now()}
l.Handler.ServeHTTP(respWr, req)
if l.DumpResponses {
buf := &bytes.Buffer{}
buf.WriteString(fmt.Sprintf("HTTP/1.1 %d %s\r\n",
respWr.status, http.StatusText(respWr.status),
))
respWr.Header().Write(buf)
writePrefixString(strings.TrimSpace(buf.String()), "<< ", os.Stderr)
}
if l.DumpErrors && isError(respWr.status) {
writePrefixString(respWr.errorOutput.String(), "<< ", os.Stderr)
}
l.writeLog(req, respWr)
}
func (l *ResponseLogger) writeLog(req *http.Request, respWr *responseWriter) {
cacheStatus := respWr.Header().Get(CacheHeader)
if strings.HasPrefix(cacheStatus, "HIT") {
cacheStatus = "\x1b[32;1mHIT\x1b[0m"
} else if strings.HasPrefix(cacheStatus, "MISS") {
cacheStatus = "\x1b[31;1mMISS\x1b[0m"
} else {
cacheStatus = "\x1b[33;1mSKIP\x1b[0m"
}
clientIP := req.RemoteAddr
if colon := strings.LastIndex(clientIP, ":"); colon != -1 {
clientIP = clientIP[:colon]
}
log.Printf(
"%s \"%s %s %s\" (%s) %d %s %s",
clientIP,
req.Method,
req.URL.String(),
req.Proto,
http.StatusText(respWr.status),
respWr.size,
cacheStatus,
time.Now().Sub(respWr.t).String(),
)
}
func isError(code int) bool {
return code >= 500
}
func writePrefixString(s, prefix string, w io.Writer) {
os.Stderr.Write([]byte("\n"))
for _, line := range strings.Split(s, "\r\n") {
w.Write([]byte(prefix))
w.Write([]byte(line))
w.Write([]byte("\n"))
}
os.Stderr.Write([]byte("\n"))
}