forked from rcrowley/go-tigertonic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
243 lines (217 loc) · 5.95 KB
/
logger.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package tigertonic
import (
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
)
// ApacheLogger is an http.Handler that logs requests and responses in the
// Apache combined log format.
type ApacheLogger struct {
*log.Logger
handler http.Handler
}
// ApacheLogged returns an http.Handler that logs requests and responses in
// the Apache combined log format.
func ApacheLogged(handler http.Handler) *ApacheLogger {
return &ApacheLogger{
Logger: log.New(os.Stdout, "", 0),
handler: handler,
}
}
// ServeHTTP wraps the http.Request and http.ResponseWriter to log to standard
// output and pass through to the underlying http.Handler.
func (al *ApacheLogger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
aw := &apacheLoggerResponseWriter{ResponseWriter: w}
al.handler.ServeHTTP(aw, r)
remoteAddr := r.RemoteAddr
if index := strings.LastIndex(remoteAddr, ":"); index != -1 {
remoteAddr = remoteAddr[:index]
}
referer := r.Referer()
if "" == referer {
referer = "-"
}
userAgent := r.UserAgent()
if "" == userAgent {
userAgent = "-"
}
username, _, _ := httpBasicAuth(r.Header)
if "" == username {
username = "-"
}
al.Printf(
"%s %s %s [%v] \"%s %s %s\" %d %d \"%s\" \"%s\"\n",
remoteAddr,
"-", // We're not supporting identd, sorry.
username,
time.Now().Format("02/Jan/2006:15:04:05 -0700"),
r.Method,
r.RequestURI,
r.Proto,
aw.StatusCode,
aw.Size,
referer,
userAgent,
)
}
// Logger is an http.Handler that logs requests and responses, complete with
// paths, statuses, headers, and bodies. Sensitive information may be redacted
// by a user-defined function.
type Logger struct {
*log.Logger
handler http.Handler
redactor Redactor
RequestIDCreator RequestIDCreator
}
// Logged returns an http.Handler that logs requests and responses, complete
// with paths, statuses, headers, and bodies. Sensitive information may be
// redacted by a user-defined function.
func Logged(handler http.Handler, redactor Redactor) *Logger {
return &Logger{
Logger: log.New(os.Stdout, "", log.Ltime|log.Lmicroseconds),
handler: handler,
redactor: redactor,
RequestIDCreator: requestIDCreator,
}
}
// Output overrides log.Logger's Output method, calling our redactor first.
func (l *Logger) Output(calldepth int, s string) error {
if nil != l.redactor {
s = l.redactor(s)
}
return l.Logger.Output(calldepth, s)
}
// Print is identical to log.Logger's Print but uses our overridden Output.
func (l *Logger) Print(v ...interface{}) { l.Output(2, fmt.Sprint(v...)) }
// Printf is identical to log.Logger's Print but uses our overridden Output.
func (l *Logger) Printf(format string, v ...interface{}) {
l.Output(2, fmt.Sprintf(format, v...))
}
// Println is identical to log.Logger's Print but uses our overridden Output.
func (l *Logger) Println(v ...interface{}) { l.Output(2, fmt.Sprintln(v...)) }
// ServeHTTP wraps the http.Request and http.ResponseWriter to log to standard
// output and pass through to the underlying http.Handler.
func (l *Logger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
requestID := l.RequestIDCreator(r)
l.Printf(
"%s > %s %s %s\n",
requestID,
r.Method,
r.URL.RequestURI(),
r.Proto,
)
for key, values := range r.Header {
for _, value := range values {
l.Printf("%s > %s: %s\n", requestID, key, value)
}
}
l.Println(requestID, ">")
r.Body = &readCloser{
ReadCloser: r.Body,
Logger: l,
requestID: requestID,
}
l.handler.ServeHTTP(&loggerResponseWriter{
ResponseWriter: w,
Logger: l,
request: r,
requestID: requestID,
}, r)
}
// A Redactor is a function that takes and returns a string. It is called
// to allow sensitive information to be redacted before it is logged.
type Redactor func(string) string
// A unique RequestID is given to each request and is included with each line
// of each log entry.
type RequestID string
// A RequestIDCreator is a function that takes a request and returns a unique
// RequestID for it.
type RequestIDCreator func(r *http.Request) RequestID
// Default RequestIDCreator implementation
func requestIDCreator(r *http.Request) RequestID {
return NewRequestID()
}
// NewRequestID returns a new 16-character random RequestID.
func NewRequestID() RequestID {
return RequestID(RandomBase62Bytes(16))
}
type apacheLoggerResponseWriter struct {
http.Flusher
http.ResponseWriter
Size int
StatusCode int
}
func (w *apacheLoggerResponseWriter) Flush() {
if f, ok := w.ResponseWriter.(http.Flusher); ok {
f.Flush()
}
}
func (w *apacheLoggerResponseWriter) Write(p []byte) (int, error) {
if w.StatusCode == 0 {
w.WriteHeader(http.StatusOK)
}
size, err := w.ResponseWriter.Write(p)
w.Size += size
return size, err
}
func (w *apacheLoggerResponseWriter) WriteHeader(code int) {
w.ResponseWriter.WriteHeader(code)
w.StatusCode = code
}
type readCloser struct {
io.ReadCloser
*Logger
requestID RequestID
}
func (r *readCloser) Read(p []byte) (int, error) {
n, err := r.ReadCloser.Read(p)
if 0 < n && nil == err {
r.Println(r.requestID, ">", string(p[:n]))
}
return n, err
}
type loggerResponseWriter struct {
http.Flusher
http.ResponseWriter
*Logger
request *http.Request
requestID RequestID
wroteHeader bool
}
func (w *loggerResponseWriter) Flush() {
if f, ok := w.ResponseWriter.(http.Flusher); ok {
f.Flush()
}
}
func (w *loggerResponseWriter) Write(p []byte) (int, error) {
if !w.wroteHeader {
w.WriteHeader(http.StatusOK)
}
if '\n' == p[len(p)-1] {
w.Println(w.requestID, "<", string(p[:len(p)-1]))
} else {
w.Println(w.requestID, "<", string(p))
}
return w.ResponseWriter.Write(p)
}
func (w *loggerResponseWriter) WriteHeader(code int) {
w.wroteHeader = true
w.Printf(
"%s < %s %d %s\n",
w.requestID,
w.request.Proto,
code,
http.StatusText(code),
)
for name, values := range w.Header() {
for _, value := range values {
w.Printf("%s < %s: %s\n", w.requestID, name, value)
}
}
w.Println(w.requestID, "<")
w.ResponseWriter.WriteHeader(code)
}